Have you ever needed to find all computer accounts in a specific OU? You can try using Active Directory Users and Computers, but it is slow and inflexible. Another way to go is firing up PowerShell, loading the Active Directory PowerShell module and writing a script.  When you need to retrieve a list of computer names, Get-ADComputer is the most useful cmdlet. You might want to restrict the output of your query by using –Filter and specifying the target OU via –SearchBase. And don’t forget to use Write-Host to make your report that much special! Add Export-Csv at the end, run the script, and then open the resulting csv file to examine the results of the commands you just wrote.

1. Open the

Powershell ISE

→ Run the following script, adjusting the OU and path for the export:

$OUpath = 'ou=Managers,dc=enterprise,dc=com'
$ExportPath = 'c:\data\computers_in_ou.csv'
Get-ADComputer -Filter * -SearchBase $OUpath | Select-object
DistinguishedName,DNSHostName,Name | Export-Csv -NoType $ExportPath

2. Open the file produced by the script in MS Excel.


Using command prompt:

To export all computers in’s servers OU to machines.txt :

DSQUERY COMPUTER "OU=servers,DC=mydomain,DC=com" -o rdn -limit 1000 > c:\machines.txt

See Also