Home » PowerShell » Get-AdOrganizationalUnit Computers – Find Computers in OU

Get-AdOrganizationalUnit Computers – Find Computers in OU

Computers in an organizational unit (OU) in the Active Directory can be retrieved using Get-AdOrganizationalUnit with Filter and SearchBase parameters to search within OU and its sub ous. Get-AdComputer gets a list of computers in the OU. It displays and exports the list of computers to a CSV file.

Get-AdOrganizationUnit in PowerShell retrieves one or more Active Directory organizational units. SearchBase parameter get’s the OU underneath the specified OU using filter parameter.

In this article, I will discuss how to find computers in the organizational unit (OU) of the Active Directory.

Find Computers in OU

OrganizationalUnit in the Active Directory contains users, computers, or other AD objects.

Using Get-AdOrganizationalUnit in PowerShell, you can get the list of computers in OU and its sub ou.

Let’s practice how to list computers in OU with an example.

$OUList = (Get-ADOrganizationalUnit -Filter * -SearchBase 'OU=SALES,DC=SHELLPRO,DC=LOCAL' -SearchScope Subtree) | select DistinguishedName

Write-Host "OU and SUb OU List" $OUList

$OUList | foreach {

$ComputerList = Get-ADComputer -Filter * -SearchBase $_.DistinguishedName -SearchScope OneLevel | Select-Object Name,DistinguishedName

Write-Host "Computers in OU" $ComputerList

  if($ComputerList)
  {

       Write-Host "Computers are exists in OU"
       $ComputerList | Export-Csv -Path "C:\PowerShell\OU-ComputersList.csv" -NoTypeInformation}
  else
  {
       Write-Host "No Computers are exists in OU"
  }
}

In the above PowerShell script to find computers in OU, follow the below steps.

  • Use Get-AdOrganizationalUnit filter OU and subou using SearchBase parameter and store it in $OUList.
  • $OUList contains distinguishedname of the OU and its subou is exists.
  • Use Foreach loop to iterate over list of OU
  • Use Get-AdComputer to get list of computers in OU specified by distinguishedname in the SearchBase parameter and store it in $ComputerList
  • Print the name of computers with their name and distinguishedname on the console.
  • Check if Computer exists in the OU
  • Using Export-CSV in PowerShell to export the list of computers in OU to csv file at the path specified.

The output of the above PowerShell script to export organizational unit computes in the CSV file is:

"Name","DistinguishedName"
"OPER-2","CN=OPER-2,OU=SALES,DC=SHELLPRO,DC=LOCAL"
"OPER-3","CN=OPER-3,OU=SALES,DC=SHELLPRO,DC=LOCAL"
"CORP-IT-E12","CN=CORP-IT-E12,OU=SALES,DC=SHELLPRO,DC=LOCAL"

The output of the PowerShell script to display computers in OU on the console is:

Get-AdOrganizationalUnit Computers - OU
Get-AdOrganizationalUnit Computers – OU

Cool Tip: How to find the empty organizational units in the Active Directory!

Conclusion

I hope you may like the above article to get adorganizational unit computers and export them to a CSV file.

We have used the Get-AdOrganizationalUnit cmdlet in PowerShell to get a list of OU and sub OU and using Get-AdComputer in PowerShell, it finds the computers in the OU.

Cool Tip: How to Create Organizational Unit in AD using PowerShell!

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