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,Enabled,LastLogonDate
Export all AD Computers to CSV file
You can also export the computer list to csv file by using the powershell cmdlet Export-CSV.
Get-ADComputer -Filter * -Properties * | Select -Property Name,DNSHostName,Enabled,LastLogonDate | Export-CSV "C:\AllComputers.csv" -NoTypeInformation -Encoding UTF8
Get all computers in OU
We can also find and get a list of AD computers from particular OU by setting target OU scope by using the parameter SearchBase. The following powershell command select all AD computers from the Organization Unit ‘TestOU‘ and export it to CSV file.
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
Apply SQL Like filter to get specific computers
The Get-ADComputer cmdlet supports SQL like filter, users who are not familiar with LDAP filter can easily use this filter to get only specific set of AD computers. The following powershell commnd export all computers based on operatingSystem that contains the value ‘Windows 7’.
Get-ADComputer -Filter 'operatingSystem -like "*Windows 7*"' -Properties * | Select -Property Name,DNSHostName,operatingSystem,LastLogonDate | Export-CSV "C:\Windows7Computers.csv" -NoTypeInformation -Encoding UTF8
Apply LDAP Filter to get specific computers
If your are familiar with LDAP filter, instead of normal filter, you can also use LDAP filter in Get-ADComputer cmdlet with more flexibility to filter Active Directory computers. The following powershell commnd get list of computers based on operatingSystem that contains the value ‘Windows 7’.
Get-ADComputer -LDAPFilter '(operatingSystem=*Windows 7*)' -Properties * | Select -Property Name,DNSHostName,operatingSystem,LastLogonDate | Export-CSV "C:\Windows7Computers.csv" -NoTypeInformation -Encoding UTF8