Thursday, October 17, 2013

Query Active Directory for a particular User

If you just want to know, for example, where a given user exists in your Active Directory, then searching for an account is a snap:
--- Begin code -----
cls
# sending LDAP query to Active Directory
#Change sAMAccountName for your query
$searcher = [ADSISearcher]'(&(objectClass=User)(objectCategory=person)(sAMAccountName=jpiggee*))'
# finding first match
$searcher.FindOne()
# finding ALL matches
$searcher.FindAll()
#This would find all user accounts with a SamAccountName that starts with "tobias". You can now use this approach to easily find out where an account is located:
# find account location
$searcher.FindAll() | Select-Object -ExpandProperty Path

---End Code ----

Joe Piggee

No comments:

Post a Comment