Finding AD User Accounts

by Oct 16, 2013

There are modules and cmdlets to deal with Active Directory tasks, but sometimes it is easier and faster to simply use some .NET code instead.

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:

# sending LDAP query to Active Directory
$searcher = [ADSISearcher]'(&(objectClass=User)(objectCategory=person)(sAMAccountName=tobias*))'
# finding first match
# finding ALL matches

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 

Twitter This Tip! ReTweet this Tip!