One of my network admin friends needed an easy way to provide some users with a list of names vs AD account names.
In many organizations, this is easy to guess, for example if my name is Jack Basement, my id might be jbasement, but in this case, it wasn’t that easy so we needed to go to AD.
There are AD cmdlets, but they are not in powershell by default.
If you have the Remote Server Administration Tools for Windows 7 installed, then you’ll find a link to “Active Directory Module for Windows PowerShell” in your administrator tools menu.
Using that we can easily get a list of the users needed and select just the columns we want
for example
Get-ADUser -identity domainuser #gets info on that user
Get-ADUser -filter {name - like "jack*"} #returns all the people named Jack
We can combine that with the select statement such as this:
Get-ADUser -filter {name - like "jack*"} | Select name, SamAccountname
Which gives us a nice list
and
Get-ADUser -filter {name - like "jack*"} | Select name, SamAccountname | convertto-csv
which will out put it as a comma separated CSV (Perfect for importing into Excel)
and
Get-ADUser -filter {name - like "jack*"} | Select name, SamAccountname | convertto-csv | out-file userlist.txt
which outputs the same thing, but to a file.
Now one neat trick, is that often you want to output all the users of a group in AD (technically this is called an Organizational Unit, or OU)
There is an LDAP filter type we can use for this
Whats cool here is that LDAP filters are sometimes a pain to get “just right” so we can cheat:
We can use the distinguished name of a known user in that group and grab the group from that
so for example
Get-ADUser -identity domainbJack
results in a bunch of output, the first field is the distingished name and we can copy and paste that for our next command
Get-ADUser -filter * -SearchBase = "OU=mygroup,DC=basementjack,DC=com"
this outputs all the users in that OU
again we can chain for flexibility
Get-ADUser -filter * -SearchBase = "OU=mygroup,DC=basementjack,DC=com" select name, SamAccountName | sort-object name
Lastly don’t forget get-help
Get-Help Get-ADUser -examples
shows a few good examples.