Active Directory: Does Specified User Exist?

This code has been in my drafts box for 2 years. I figured I’d just go ahead and publish it, though I’m unsure of the context now.

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(objectcategory=domainDNS)"
$result = $search.FindOne()
$domain = $result.GetDirectoryEntry()
$domain |fl *

if ($result -ne $null)
{
$user = $result.GetDirectoryEntry()
Write-Host $user.name " exists"
}
else {Write-Host $struser " Does not exist"}

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in Active Directory, PowerShell

Leave a Reply