Skip to content

Instantly share code, notes, and snippets.

@vijayjt
Created April 10, 2017 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijayjt/01ce67a7e8a07a22bf11c722c8bf4738 to your computer and use it in GitHub Desktop.
Save vijayjt/01ce67a7e8a07a22bf11c722c8bf4738 to your computer and use it in GitHub Desktop.
How to search AD using PowerShell
$username = 'aduser'
$password = 'replaceWithPassword'
$DomainControllerIpAddress = '192.168.0.21'
$LdapDn = 'dc=acme,dc=local'
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://$($DomainControllerIpAddress):389/$LdapDn",$username,$password)
# Here look for a user
$ds = new-object System.DirectoryServices.DirectorySearcher($dn)
$ds.filter = "((userPrincipalName=*))"
$ds.SearchScope = "subtree"
$ds.PropertiesToLoad.Add("distinguishedName")
$ds.PropertiesToLoad.Add("sAMAccountName")
$ds.PropertiesToLoad.Add("lastLogon")
$ds.PropertiesToLoad.Add("telephoneNumber")
$ds.PropertiesToLoad.Add("memberOf")
$ds.PropertiesToLoad.Add("distinguishedname")
$ds.PropertiesToLoad.Add("otherHomePhone");
$ds.FindAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment