Getting AD User Data via PowerShell
It’s a common question asked of IT – “Can you give me a list of who’s in Marketing?” or “How many accounts do we actually have?”
Before PowerShell, this was a lot harder to do. There were companies like Quest Software who provided several handy tools (and still do) , or long complicated visual basic scripts.
So, how do you get a list of users? All of this is being done from the Active Directory Module for Windows PowerShell which will install as part of the Windows Server 2012 Feature – Role Administration Tools > AD DS and AD LDS Tools > Active Directory Module for Windows PowerShell.
The ‘Get-ADUser’ command is what we’ll use to demonstrate what you can do.
For starters, ‘Get-ADUser -filter*’ will get you a list of all users, and they’ll output in this format one after the other:
A lot of information. You can specify a single user with:
Get-ADUser -identity username
which will just show you the one result.
As you may be aware, there are a lot more fields a user has than just the ones shown. You can tell PowerShell to show you all the properties by modifying the command like this:
Get-ADUser -identity username -properties *
Note that in PowerShell v4 if you get the error “get-aduser : One or more properties are invalid.” then there may be an issue with your schema. Check out this post for more information.
If there’s just one extra property you need, there’s no point getting everything, so if you needed to see a field such as “Department” for all users then adjust the command like this:
Get-ADUser -filter * -properties Department
Now, this gives the results for every single user in your Active Directory environment. You can narrow this down to a particular OU (and consequent sub OUs) by changing the command to this:
Get-ADUser -searchbase “ou=specialusers,ou=users,dc=mydomain,dc=com” -filter * -Properties Department
Now, you might be wondering how to get rid of all the standard properties and only see the ones you want.
There are two ways to pipe out the data that you want. One is with the ‘Format Table’ and the other is ‘Select Object’. Say you want a list of staff and their departments, we only need to use the ‘name’ field and the ‘department’ field.
Here’s what the two command look like, which are very similar:
Get-ADUser -searchbase “ou=specialusers,ou=users,dc=mydomain,dc=com” -filter * -Properties Department | ft name, department
Get-ADUser -searchbase “ou=specialusers,ou=users,dc=mydomain,dc=com” -filter * -Properties Department | Select-Object name, department
The results of these commands will look exactly the same. But, when you want to export this information out, you would normally use the ‘Export-CSV’ command. If you use the ‘ft’ option, the results will not be what you expect. There is a brief writeup on this on the Windows PowerShell Blog which shows what you’ll see and explains why. The ‘Select Object’ command doesn’t have this issue.
So, if you want to output this list to a text file, here’s the command to use:
Get-ADUser -searchbase “ou=specialusers,ou=users,dc=mydomain,dc=com” -filter * -Properties Department | Select-Object name, department | export-csv c:\temp\myfile.csv
Note that you can also cheat and just pipe any output to a textfile using the old DOS redirect output method, which works even with the ‘ft’ option:
Get-ADUser -searchbase “ou=specialusers,ou=users,dc=mydomain,dc=com” -filter * -Properties Department | Select-Object name, department > c:\temp\myfile.csv
Note that one ‘>’ creates a new file or overwrites an existing, while a double ‘>>’ will create a new file or append to an existing.
Easy! Now you can provide Active Directory details to whomever asks, with a one line command that will output only the fields you want.
- Review: Lumia 830 vs Lumia 930 showdown - December 13, 2014
- Western Digital Make Backup Devices? - September 14, 2014
- Review: Nokia Lumia 930 - September 4, 2014
- KMS and MAK Licensing - July 31, 2014
- Review: Samsung Gear 2 Neo - June 18, 2014
- Storage that’s nearly INVINCIBLE - June 2, 2014
- Coping with infinite email - May 30, 2014
- TechEd North America – The Closing Line - May 18, 2014
- TechEd North America – Half Way Mark - May 15, 2014
- Windows Phone 8.1 Is Out! - April 21, 2014