Get all computers from OU in AD using PowerShell

We can get a list of all computers in Active Directory using the Powershell cmdlet Get-ADComputer. In this article, I am going to write powershell script to find and get a list of all computers from ceratin OU in AD and export computer details to csv file.

Before proceed run the following command to import Active Directory module.

Import-Module ActiveDirectory

The following command find and list all the available computers in Active Directory.

Get-ADComputer -Filter * -Properties * |
 Select -Property Name,DNSHostName,LastLogonDate

Get list of all computers in OU

We can find and get a list of all computers from a certain OU by setting target OU scope by using the parameter SearchBase. The following powershell command select all computers from the Organization Unit ‘TestOU’.

Get-ADComputer -Filter * -SearchBase "OU=TestOU,DC=TestDomain,DC=com" -Properties *  |
 Select -Property Name,DNSHostName,LastLogonDate

Export all computers in OU to CSV

We can export all computer details to csv file by using the powershell cmdlet Export-CSV.

Get-ADComputer -Filter * -SearchBase "OU=TestOU,DC=TestDomain,DC=com" -Properties *  |
 Select -Property Name,DNSHostName,Enabled,LastLogonDate | 
 Export-CSV "C:\AllComputersInOU.csv" -NoTypeInformation -Encoding UTF8

Advertisement

Leave a Comment