Finding AD Users

by Aug 1, 2014

All PowerShell versions

Searching the AD can be done with simple calls provided you are logged on an Active Directory domain. In a previous tip we illustrated the basic script. Here is an extension that allows you to define a search root (starting point for your search), as well as a flat search (rather than recursing into a container).

It also illustrates how an Active Directory search result is turned into the actual user object:

$SAMAccountName = 'tobias'
$SearchRoot = 'LDAP://OU=customer,DC=company,DC=com'
$SearchScope = 'OneLevel'

$ldap = "(&(objectClass=user)(samAccountName=*$SAMAccountName*))"
$searcher = [adsisearcher]$ldap
$searcher.SearchRoot = $SearchRoot
$searcher.PageSize = 999
$searcher.SearchScope = $SearchScope

$searcher.FindAll() | 
  ForEach-Object { $_.GetDirectoryEntry()  } | 
  Select-Object -Property *

Twitter This Tip! ReTweet this Tip!