Export Active Directory information with PowerShell

Requirements and installation

For first time and one time setup you need to do the following:

  1. DownloadRemote Server Administration Tools for Windows 10 from Microsoft and install it. Make sure you choose the one appropriate for your operating system.

  2. In Windows run OptionalFeatures.exe and turn on Remote Server Administration Tools.

  3. In PowerShell you will have to run Import-Module ActiveDirectory

Usage

Export all users in AD with all attributes to a csv file.

Make sure you Set-Location -Path or cd to the export path first.

Get-ADUser -Filter * -Properties * | output.csv -Encoding unicode

Export users of a spesific organizational unit:

If your domain is adatum.local then you should write DC=adatum,DC=local. For some strange reason the list of OU’s and DCs must be listed in a reverse order like this:

Get-ADUser -filter * -SearchBase "OU=Finance,OU=Users,OU=Adatum,DC=adtm,DC=local" -Properties "DisplayName","EmailAddress" | select DisplayName,EmailAddress

But we could make it more tidy by setting the list of OU and DC as variables. We can also make a variable with attributes. This makes it easier to change or adapt the script to different tasks.

$list = "OU=Finance,OU=Users,OU=Adatum,DC=adtm,DC=local"
$attributes = "DisplayName","EmailAddress"
Get-ADUser -filter * -SearchBase $list -Properties $attributes | select $attributes

Export users that are actually human employees

A lot of times you might have some service accounts, test accounts and so on. If you just want to export a list of actual employees you can filter the results by using any attribute such as the employee number or employee ID.

Get-ADUser -Filter 'employeeID -like "*"' -properties DisplayName,mobile,EmailAddress | select DisplayName,mobile,EmailAddress

Get all diabled users that have an employee number

Get-ADUser -Filter {employeeID -like "*" -AND enabled -eq $false} -properties DisplayName,EmailAddress | select DisplayName,EmailAddress