0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Export AD Users to CSV File

Use the PowerShell command Get-ADUser -Filter * -Properties EmailAddress | Select-Object Name,SamAccountName,EmailAddress | Export-Csv -Path 'users.csv' -NoTypeInformation to export all AD users with selected properties to a CSV file.
📋

Examples

InputExport all AD users with Name and Email
OutputCSV file 'users.csv' containing columns Name and EmailAddress for all users
InputExport AD users with SamAccountName and Enabled status
OutputCSV file 'users.csv' with SamAccountName and Enabled columns for all users
InputExport AD users filtered by department 'Sales'
OutputCSV file 'users.csv' with users only from Sales department
🧠

How to Think About It

To export AD users to CSV, first get the list of users from Active Directory using Get-ADUser. Then select the user properties you want to save. Finally, export the selected data to a CSV file using Export-Csv.
📐

Algorithm

1
Use Get-ADUser with a filter to get all or filtered users
2
Select the desired user properties to include
3
Export the selected user data to a CSV file
💻

Code

powershell
Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties EmailAddress | Select-Object Name,SamAccountName,EmailAddress | Export-Csv -Path 'users.csv' -NoTypeInformation
Write-Output "Export completed: users.csv"
Output
Export completed: users.csv
🔍

Dry Run

Let's trace exporting all AD users with Name and EmailAddress to CSV

1

Get all AD users

Get-ADUser -Filter * -Properties EmailAddress returns a list of user objects with many properties including EmailAddress

2

Select properties

Select-Object Name,SamAccountName,EmailAddress extracts only these fields

3

Export to CSV

Export-Csv writes the selected data to 'users.csv' without type info

NameSamAccountNameEmailAddress
Alice Johnsonajohnsonalice.johnson@example.com
Bob Smithbsmithbob.smith@example.com
💡

Why This Works

Step 1: Get-ADUser fetches users

The Get-ADUser cmdlet retrieves user accounts from Active Directory based on the filter.

Step 2: Select-Object picks fields

Using Select-Object lets you choose which user properties to include in the output.

Step 3: Export-Csv saves data

The Export-Csv cmdlet writes the selected user data into a CSV file for easy use.

🔄

Alternative Approaches

Export all user properties
powershell
Get-ADUser -Filter * -Properties * | Export-Csv -Path 'all_users.csv' -NoTypeInformation
Write-Output "Export completed: all_users.csv"
Exports every property but creates a large CSV file, which may be slower and harder to read.
Filter users by department
powershell
Get-ADUser -Filter {Department -eq 'Sales'} -Properties EmailAddress | Select-Object Name,EmailAddress | Export-Csv -Path 'sales_users.csv' -NoTypeInformation
Write-Output "Export completed: sales_users.csv"
Exports only users from the Sales department, useful for targeted reports.
Use LDAP filter for complex queries
powershell
Get-ADUser -LDAPFilter '(objectClass=user)' -Properties EmailAddress | Select-Object Name,EmailAddress | Export-Csv -Path 'ldap_users.csv' -NoTypeInformation
Write-Output "Export completed: ldap_users.csv"
LDAP filters allow more complex queries but require LDAP syntax knowledge.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each user once, so time grows linearly with the number of users.

Space Complexity

Memory usage grows with the number of users because all selected user data is stored before exporting.

Which Approach is Fastest?

Filtering users before selecting properties reduces data size and speeds up export compared to exporting all properties.

ApproachTimeSpaceBest For
Select specific propertiesO(n)O(n)Balanced speed and file size
Export all propertiesO(n)O(n)Complete data but slower and large files
Filter users by attributeO(m) where m ≤ nO(m)Targeted exports with fewer users
💡
Always use -NoTypeInformation with Export-Csv to avoid extra type info in your CSV file.
⚠️
Forgetting to import the ActiveDirectory module or running PowerShell without the required permissions causes errors.