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

Export memeberships of users with employee number

Find all employees with employee number and export their respective memberships to individual text files

# Find all users in AD with emplyee number and export a text file for each user with their respective memberships.

$users = Get-ADUser -Filter 'employeeID -like "*"'
foreach ($user in $users) {
    $filename = $(get-date -f yyyy-MM-dd-HH-mm-ss) + ' - ' + $user.Name     
    (Get-ADUser –Identity $user –Properties MemberOf).MemberOf | Out-File C:\your_folder\$filename.txt
}

Export all attributes of users with employee number

Find all employees with employee number and export their all their respective attributes to individual text files

# Find all users in AD with emplyee number and export a text file for each user with their respective memberships.

$users = Get-ADUser -Filter 'employeeID -like "*"'
foreach ($user in $users) {
    $filename = $(get-date -f yyyy-MM-dd-HH-mm-ss) + ' - ' + $user.Name
    (Get-ADUser –Identity $user –Properties *) | Out-File C:\your_folder\$filename.txt
}

Export a TXT file for each user with containing attributes of your choice

# Search the "whole shebang" or filter specific attributes?

In this example it exports TXT files containing mobile number from AD.


$variable1 = Get-ADUser -Filter 'employeeID -like "*"'
# Make output folder if it does not exist
$path = "$home\Desktop\PowerShell-export\" ; md $path -Force
# For each - do this
foreach ($user in $variable1) {
    # How should each file be named?
    $filename = $user.Name
    # What would you like each file to contain and where to save them?
    (Get-ADUser –Identity $user –Properties mobile).mobile | Out-File "$path\$filename.txt"
}