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 a CSV file with all attributes, analyze aduser all attributes, and prepare aduser reports.

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

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

How to Export Active Directory Users to CSV

To export active directory users to a CSV file, use the Get-Cmdlet. This command fetches the user object information from the Active Directory and uses the -Properties * parameter to display all attributes. Lastly, the user information is exported to a CSV file using the Export-CSV cmdlet.

Get-Aduser -filter * -Properties * | Export-CSV -Path "C:\PowerShell\export_adusers_info.csv"

In the above PowerShell script, the Get-AdUser cmdlet uses the Filter parameter that specifies retrieving all the users from the Active Directory and pipes the result to the Export-CSV cmdlet. The Export-CSV command uses the Path parameter to specify the location to export ad users to csv file.

Export Ad Users with All Attributes

Use the Get-AdUser cmdlet in PowerShell to retrieve the user account information from the Active Directory along with the Properties * parameter to fetch all of the attributes set on the user object. The Out-File command will export the ad user with all attributes to csv file.

 Get-AdUser -Identity toms -Properties * |Out-File "aduser_all_attributes.csv"

In the above PowerShell script, the Get-AdUser command uses the Identity parameter to specify the username for which we want to retrieve all attributes, in this case, “toms”.

The Get-AdUser command uses the Properties * parameter to fetch all of the attributes for the user object and pipe the result to the Out-File command. The Out-File command exports the aduser with all attributes to the CSV file “aduser_all_attributes.csv”.

The output of the above PowerShell script that exports the aduser attribute to csv is given below.

Export Ad Users with All Attributes in PowerShell
Export Ad Users with All Attributes in PowerShell

Export Ad User Name to CSV

To export the aduser name to a CSV file, use the Get-AdUser cmdlet in the active directory with the 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 the Export-Csv command, it exports the aduser name to a 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 follows.

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 the pipe operator (|)

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

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

Cool Tip: How to get ad user attributes in PowerShell!

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 the active directory OU to the 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 an aduser from the active directory OU (organizational unit) using the SearchBase parameter.

The output of the first command is below.

Get AdUser from OU
Get Active Directory Users from OU

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

Export Enabled Ad Users to CSV

Active directory user Enabled property has either a 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 the Export-Csv cmdlet in PowerShell, it exports enabled ad users to a CSV file.

Export User logon Name from Active Directory

To export user logon name, lastlogondate from the active directory, use the 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 on exporting ad user to a CSV file with multiple attributes like name, email address, logon name, and last logon date is helpful to you.

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