Export AD Computers to CSV using PowerShell

We can generate and export Active Directory computers report to CSV file using powershell cmdlets Get-ADComputer and Export-CSV. The Get-ADComputer cmdlet supports SQL like filter and LDAP filter to filter AD Computers.

Find and List AD Computers

The following powershell script list the selected properties of all computers.

Import-Module ActiveDirectory
Get-ADComputer -Filter * -Properties * |
 Select -Property Name,operatingSystem,@{Name="LastLogon";
Expression={[DateTime]::FromFileTime($_.lastLogon).ToString()}}

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 script select computers whose OperatingSystem contains the text ‘Server 2008 R2’.

Import-Module ActiveDirectory
Get-ADComputer -Filter 'OperatingSystem -like "*Server 2008 R2*"' -Properties * |
 Select -Property Name,operatingSystem,@{Name="LastLogon";
Expression={[DateTime]::FromFileTime($_.lastLogon).ToString()}}

Apply LDAP Filter to get specific set of AD computers:

If your are familiar with LDAP filter, instead of normal filter, you can also use LDAP filter with more flexibility to filter Active Directory computers. The below script select all the computers whose OperatingSystem contains the text ‘Server 2008 R2’.

Import-Module ActiveDirectory
Get-ADComputer -LDAPFilter '(OperatingSystem=*Server 2008 R2*)' -Properties * |
 Select -Property Name,operatingSystem,@{Name="LastLogon";
Expression={[DateTime]::FromFileTime($_.lastLogon).ToString()}}

Export AD Computers to CSV file

We can generate and export all computer details to CSV file by using Export-CSV cmdlet. You can add more attributes in Select -Property field to export more AD attributes.

Import-Module ActiveDirectory
Get-ADComputer -Filter 'OperatingSystem -like "*Server 2008 R2*"' -Properties * |
 Select -Property Name,operatingSystem,@{Name="LastLogon";
Expression={[DateTime]::FromFileTime($_.lastLogon).ToString()}} |
# Export AD Computer Report to CSV file 
Export-CSV "C:\ADComputers.csv" -NoTypeInformation -Encoding UTF8

Export AD Computers from Specific OU

We can set target OU scope by using the parameter SearchBase. The following powershell script select all the Windows Server 2008 R2 AD computers from the Organization Unit ‘TestOU’ and export it to CSV file.

Import-Module ActiveDirectory
Get-ADComputer -SearchBase "OU=TestOU,DC=TestDomain,DC=Local"`
-Filter 'OperatingSystem -like "*Windows Server 2008 R2*"' -Properties * |
 Select -Property Name,operatingSystem,@{Name="LastLogon";
Expression={[DateTime]::FromFileTime($_.lastLogon).ToString()}} |
# Export AD Computers to CSV file 
Export-CSV "C:\ADComputers.csv" -NoTypeInformation -Encoding UTF8

CSV output of AD Computers Report:

Powershell - Export Active Directory Computers to CSV

Advertisement

Leave a Comment