Get-ADComputer-Find Computer Details in OU with Examples

In this article, I will walk you through how to use PowerShell Get-AdComputer cmdlet to find one or more computers in the Active Directory or find computers in OU (Organization Unit). Using PowerShell Get-AdComputer to get list of computers in OU, get adcomputer filter by the operating system.

Get-AdComputer performs a search to retrieve multiple computers in the active directory or return a single computer as well. Get-AdComputer cmdlet retrieves the default set of computer object properties.

You can use the Get-AdComputer Properties parameter to retrieve additional properties. PowerShell Get-AdComputer Cmdlet has an Identity parameter to retrieve specific computer objects from Active Directory.

If you want to retrieve one or more computer objects based on search criteria, use Filter or LDAPFilter parameters. Using get-adcomputer filter and get-adcomputer ldapfilter, you can get AD computer in specific OU.

Prerequisites

To use the Get-AdComputer cmdlet, your system needs to have the following requirement:

  • PowerShell Active Directory module to be installed
  • User with administrator access or have enough access to read Active Directory information

Tip: To know about which PowerShell modules are available in the system, run the below command in PowerShell ISE

Get-Module -ListAvailable

This command returns all the modules installed and available in the system. If an Active Directory module is not available then follow Active Directory installation steps.

Cool Tip: How to get certificates using PowerShell!

Get-AdComputer Syntax

This PowerShell Get-Adcomputer cmdlet returns one or more computer objects from Active Directory.

Syntax

Get-ADComputer
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   -Filter <String>
   [-Properties <String[]>]
   [-ResultPageSize <Int32>]
   [-ResultSetSize <Int32>]
   [-SearchBase <String>]
   [-SearchScope <ADSearchScope>]
   [-Server <String>]
   [<CommonParameters>]

Get-ADComputer
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   [-Identity] <ADComputer>
   [-Partition <String>]
   [-Properties <String[]>]
   [-Server <String>]
   [<CommonParameters>]

Get-ADComputer
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   -LDAPFilter <String>
   [-Properties <String[]>]
   [-ResultPageSize <Int32>]
   [-ResultSetSize <Int32>]
   [-SearchBase <String>]
   [-SearchScope <ADSearchScope>]
   [-Server <String>]
   [<CommonParameters>]

Using Identity Parameter

Get-AdComputer Identity the parameter is used to get a single computer object from Active Directory.

You can identify a computer name by using

  • GUID
  • Distinguishedname
  • SAM Account name
  • Security Identifier (SID)

Let’s consider an example if you have a computer named it-2020 and want to retrieve it using Get-AdComputer cmdlet, use below command

Get-AdComputer -Identity "it-2020"

Above get-adcomputer command get computer name it-2020 specified by Identity parameter in active directory and returns with the default set of adcomputer properties.

In our next example, if you want to get-adcomputer all properties for a given it-2020 computer object, run the below command

Get-AdComputer -Identity "it-2020" -Properties *

In the above code, the command will get ad computer properties, if you want to return a few properties, you can specify them as comma-delimited.

Cool Tip: Do you know the equivalent of cat command in Windows!

Get-AdComputer Examples

Get all computer with a name start like or starting with particular string

Very often administrator wants to get computer objects where the name starts with a particular string. You can use the Get-Adcomputer Filter property to search for AD computer objects.

Get-ADComputer -Filter 'Name -like "it-20*"' -Properties *

In the above get-adcomputer example script, the command gets ad computer properties which name start with it-20, and returns all the properties for computer objects.

Cool Tip: Learn how to get aduser using userprincipalname!

Get Ad Computer Filter – get all computers in Active Directory

Get-ADComputer -Filter *

In the above get-adcomputer example, PowerShell get-adcomputer filter with *(wildcard) returns all computers objects.

Get-AdComputer Multiple Filters – multiple names

You can search for computers where adcomputer name like or name starts with a certain string using below command

Get-ADComputer -Filter 'Name -like "it-20*" -or Name -like "it-21*"' -Properties *

In the above get-adcomputer example, it returns one or more computers where names like it-20 or it-21 based on get-adcomputer filter name like property.

Get-AdComputer Multiple Filters – PasswordLastSet in last 15 days

You can get computers from the active directory where the computer name starts with a specified name and password changes in the last 15 days using the below command

$lastpwddate = [DateTime]::Today.AddDays(-15);
Get-ADComputer -Filter 'Name -like "it-20*" -and PasswordLastSet -ge $lastpwddate ' -Properties *

In the above get-adcomputer example, get-adcomputer filter name like ‘it-20*’ to get all the computer name start with it-20 and password changes in last 15 days.

Cool Tip: Learn how to download zip files using PowerShell!

Get-AdComputer Filter by Windows 2016 Operating System

$os = Get-ADComputer -Filter * -Properties OperatingSystem -ResultPageSize 500
$os | where OperatingSystem -eq "Windows Server 2016" | select name

This command gets all the different OS system found in active directory and then apply get adcomputer filter to get a list of computers having Operating System is equal to Windows Server 2016.

In the above example, get-adcomputer filter operating system if it is equal to Window Server 2016 and gets a list of all computers.

Find computers in OU

Get-AdComputer has a SearchBase parameter to limit the search to specific to an OU or its child OU.

To find computers in OU using the get-adcomputer searchbase parameter in PowerShell, use below code

Get-ADComputer -Filter * -SearchBase 'OU=Sales, DC=Shellgeek, DC=com'

Above PowerShell get adcomputer filter ou script, find computers specific to an organizational unit in Active Directory.

Get list of computers in OU and export to csv

To get a list of computers in OU and export the computer list from the active directory to a CSV file, use below PowerShell script

$OULocation = 'ou=Sales,dc=shellgeek,dc=com'
$csvpath = 'C:\powershell\computers_in_ou.csv'
Get-ADComputer -Filter * -SearchBase $OULocation  | Select-object
DistinguishedName,DNSHostName,Name | Export-Csv -NoType $csvpath 

In the above get-adcomputer example, we have set OU location and CSV path.

Get-AdComputer filter parameter restricts the output of the query using filter and specifying SearchBase parameter to search in specific OU. Later it gets a list of computers in OU and using Export-Csv exports the computer list from the active directory to the CSV file.

Cool Tip: Using set-adgroup to modify active directory group attributes PowerShell!

Using Get-AdComputer LDAPFilter parameter

LDAPFilter parameter is used to retrieve or more computers.

Let’s consider an example, if you have a requirement to find computers in OU, use the below command

Get-ADComputer -LDAPFilter "(name=*it-20*)" -SearchBase "CN=Computers,DC= Sales,DC=com"

This command returns the computers from location CN= Computers, DC=Sales, DC=com. Get-AdComputer LDAPFilter is used with the SearchBase parameter. With the help of the SearchBase parameter, it limits the search to specific to an OU or its child OU.

Cool Tip: How to remove adcomputer PowerShell!

Get AdComputer Serial Number

You can get active directory computers serial number using Get-CimInstance cmdlet in PowerShell which uses the Win32_Bios method to get computer serial number.

 Get-ADComputer -Filter *| Select-Object Name | Foreach-Object {Get-CimInstance Win32_Bios -ComputerName $_.Name -ErrorAction SilentlyContinue | Select-Object PSComputerName,SerialNumber}

In the above PowerShell script,

Get-AdComputer command gets all active directory computers and passes the output to the second command where it gets adcomputer name only.

In the next command, it uses foreach-object to iterate over adcomputers. It uses the Get-CimInstance cmdlet to get the computer name and serial number for each active directory computer.

Get-AdComputer FAQ

How to find a computer in AD using Get-ADComputer -Filter name?


Consider an example, to find computer name ‘comp-1’ in active directory, use below command
Get-ADComputer -Filter "name -eq 'comp-1'"

In the above example, it returns the computer name if get-adcomputer filter name like ‘comp-1’ in active directory.

However, if you want to use a computer name stored in a variable and find it
$comp = "comp-1"
Get-ADComputer -Filter "name -eq '$comp'"

How to use PowerShell cmdlet get-adcomputer to filter operating system?


If you want to find all the computers having Windows 10 operating system using get-adcomputer filter, use the below command

Get-ADComputer -Filter {OperatingSystem -like ‘*Windows 10*’}

In the above PowerShell get-adcomputer example, the command returns a list of the computers if get-adcomputer filter operating system like ‘*Window 10*’

How to use Get-AdComputer filter to find computers in OU


To find computers in OU (organizational unit) using the Get-AdComputer filter parameter, use the below command

Get-ADComputer -Filter * -SearchBase “OU=Sales, DC=shellgeek, DC=com”

In the above PowerShell get-adcomputer filter examples, it returns a list of the computers in OU if get-adcomputer filter ou like Sales.

How to use Get-AdComputer Searchbase parameter to get computers from multiple OUs?


Let’s consider an example if you have multiple OU and you want to get a list of computers from multiple OUs using the Searchbase parameter, use the below command

#Store OUs in variable

$OUs= "OU=Sales,DC=shellgeek,DC=com",
"OU=HR,DC=shellgeek,DC=com",
"OU=Finance,DC=shellgeek,DC=com"

$Ous | foreach { Get-ADComputer -Filter * -SearchBase $_} | Select Name


Use foreach to iterate over multiple OU and use the SearchBase parameter to get a list of computers using the get-adcomputer filter with OU.

How to get ad computer last logon date?


Get-ADComputer -identity It-2020 -Properties * | FT Name, LastLogonDate -Autosize

This command returns get-adcomputer lastlogondate for computer name It-2020.

Read more on difference between lastlogon and lastlogontimestamp in active directory.

How to export get-adcomputer lastlogondate Export-CSV


Let’s consider an example, we have to find computer accounts that have been not logged on to Active Directory in the last 90 days, you can get list of accounts using LastLogonTimeStamp property.

# Specify timeframe
$inactiveperiod = 90

$time = (Get-Date).Adddays(-($inactiveperiod))

Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 1000 -resultSetSize $null -Properties Name, SamAccountName| Export-CSV “C:\PowerShell\InActiveComputer.csv” –NoTypeInformation

This command performs a search for those computer accounts where lastlogontimestamp not updated in the last 90 days and export all stale or inactive computer accounts to CSV file.

Cool Tip: Check ad schema version PowerShell!

Conclusion

Hope you like the above article to use PowerShell Get-AdComputer cmdlet to find computer object information from the active directory.

In the above PowerShell Get-AdComputer examples, we have seen many different examples to get list of computers based on get-adcomputer filter name or get-adcomputer filter operating system or get-adcomputer filter ou.

The above steps by steps guide on how to use Get-AdComputer cmdlet with different parameters like Identity, LDAPFilter, SearchBase with real-world examples will help you understand the use of cmdlet.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment