Export active directory users to CSV


For many reasons, we sometimes need to Export active directory users to CSV.
We can perform this operation in some ways, the two best ways are the Dsquery command and the PowerShell cmdlet Get-ADUser.

To use DSquery command to export users to CSV, we should use samAccountName and Name while Modifying the filter based on information needed as follow:
Dsquery * -filter “&(objectClass=User)(objectCategory=Person)” –attr samAccountName Name >>MyUsers.csv

To use Get-ADUser command:
Get-ADUser -Filter * -SearchBase “ou=OU,dc=Gmalaya,dc=Local” -Properties * | Export-Csv  “d:\MyUsers.csv”