Home » PowerShell » Export Ad User to CSV in PowerShell

Export Ad User to CSV in PowerShell

Active directory user has many attributes associated with it to describe ad user. As an administrator, often we export ad users to CSV file to go through user attributes, analyze aduser all attributes, prepare aduser reports.

Get-AdUser cmdlet in active directory get one or more users from active directory based on search criteria and using filter conditions get ad users from the domain, specific OU. Using Export-CSV to export ad users to CSV file with all attributes in PowerShell.

In this article, I will explain using the Get-AdUser cmdlet in PowerShell to export users from the active directory to the CSV file, export ad users email address from the active directory, export ad users with all attributes.

Export Ad Users Name to CSV

To export aduser name to CSV file, use Get-AdUser cmdlet in active directory with Filter parameter as below

Get-ADUser -Filter * | Select-Object Name | export-csv -path D:\adusers-export.csv -NoTypeInformation

In the above PowerShell script, Get-AdUser gets all users in the domain using Filter * ( wildcard character ) and using Export-Csv, it exports aduser name to CSV file.

Export AdUser Email Addresses from Active Directory to Csv

Active directory user has email address attribute, to export aduser email addresses from active directory to CSV file, use Get-AdUser cmdlet as following

Get-ADUser -Filter * -Properties * | Select Name, EMailAddress,DisplayName | Export-Csv D:\adusers-export.csv -NoTypeInformation

In the above PowerShell script, the Get-AdUser Filter parameter gets ad users from the active directory and passes it to the second command through pipe operator (|)

The second command uses Name, DisplayName, EmailAddress to export aduser email address from the active directory to the CSV file.

Above script, export ad user multiple attributes to CSV file.

Export Users from Active Directory OU to CSV

Organizational Unit (OU) is a container in the Active Directory containing users, computers, and groups.

Use Get-AdUser in PowerShell to export users from active directory OU to CSV file, as below

Get-ADUser -Filter * -SearchBase "OU=HR,DC=SHELLPRO,DC=LOCAL" -Properties * | Select Name,EMailAddress,DisplayName | Export-Csv -Path D:\adusers-export.csv -NoTypeInformation

In the above PowerShell script,

the first command gets aduser from active directory OU (organizational unit) using the SearchBase parameter.

The output of the first command as below

Get AdUser from OU
Get Active Directory Users from OU

Using the Export-CSV in PowerShell, it exports users from active directory OU to CSV file.

Export Enabled Ad Users to CSV

Active directory user Enabled property has either True or False value which decides aduser is enabled or not.

To export enabled ad users to CSV file, use Get-AdUser cmdlet as below

Get-ADUser -Filter * -Property * | Where-Object {$_.Enabled -like "True"} | Select Name, EmailAddress, DisplayName | Export-Csv -Path D:\adusers-export.csv -NoTypeInformation

In the above PowerShell script,

The first command gets adusers from the active directory and passes user objects to the second command.

Second command check Enabled status and select Name, EmailAddress, and DisplayName.

Using Export-Csv cmdlet in PowerShell, it export enabled ad users to CSV file.

Export User logon Name from Active Directory

To export user logon name, lastlogondate from active directory, use Get-AdUser cmdlet as below

 Get-ADUser -Filter * -Property * | Where-Object {$_.Enabled -like "False"} | Select Name, EmailAddress, DisplayName,LastLogonDate,UserPrincipalName | Export-Csv -Path D:\adusers-export.csv -NoTypeInformation

Above PowerShell script, get ad user name, emailaddress, displayname, lastlogondate, and userprincipalname and export user logon name and last logon date to CSV file using Export-CSV.

Conclusion

I hope the above article to export ad user to CSV file with multiple attributes like name, email address, logon name, last logon date helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment