[Active Directory(AD)] Use PowerShell and Command to export Active Directory


Traditional Commands

Commands: csvde, ldifde
AD Attributes: ADSIEDIT.msc tool

csvde -f c:\exportfilename.txt -r ("objectclass=user") -u -n -m -l "sAMAccountName,name,mail,distinguishedName,canonicalName,"

csvde -f c:\user.txt -r (and("objectclass=user")(distinguishedName!="DN")) -u -n -m -l "sAMAccountName,name,mail," -o "distinguishedName"

ldifde -f c:\user.txt -d "ou=yourOUA,dc=yourDdomainName,dc=yourDomainName" -r "(&(objectclass=user)(objectcategory=person))" -u -n -m -l "sAMAccountName,name,mail," -o "objectCategory"

note: if on Windows 7, you need to install "Remote Server Administration Tools for Windows 7 with Service Pack 1 (SP1)"


  • Get Whole Computer name in your Domain

one person wrote this script on his website following

   1: $strCategory = "computer"
   2: $objDomain = New-Object System.DirectoryServices.DirectoryEntry
   3: $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
   4: $objSearcher.SearchRoot = $objDomain
   5: $objSearcher.Filter = ("(objectCategory=$strCategory)")
   6: $colProplist = "name"
   7: $OutputFile = "c:\data\result.txt"
   8: foreach ($i in $colPropList) {$objSearcher.PropertiesToLoad.Add($i)}
   9: $colResults = $objSearcher.FindAll()
  10: foreach ($objResult in $colResults | Out-File $OutputFile ) {$objComputer = $objResult.Properties; $objComputer.name }

not bad, but probably he forgot “PowerShell ISE” has tons of CmdLet

you can use the function “GET-ADComputer” to gain the same results, so I modified like below (-SearchBase is optional)

   1: Import-Module ActiveDirectory
   2: $OutputFile = "c:\data\results.txt" 
   3: Get-ADComputer -SearchBase "DC=xxx,DC=xxx,DC=com"  -Filter * | Select Name | Out-File $OutputFile

It’s more neat and clean, don’t want a “*.txt” file? ok, we change it to be a “*.csv

   1: #export to csv File type
   2: Import-Module ActiveDirectory
   3: $OutputFile = "c:\data\results.csv" 
   4: Get-ADComputer -SearchBase "DC=xxx,DC=xxx,DC=com"  -Filter * | Select Name | Export-Csv $OutputFile

you can just modified what you need, for example

  • Get OS type + Computer Name + version + Service Pack version

   1: #Export ComputerName, OS, ServicePack, OS version
   2: Import-Module ActiveDirectory
   3: $OutputFile = "c:\data\results.csv"
   4: Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion | Export-CSV $OutputFile -NoTypeInformation -Encoding UTF8

Newer Older