Home » PowerShell » Find Empty Organizational Unit in Active Directory

Find Empty Organizational Unit in Active Directory

Empty organizational units in the Active Directory can be retrieved using Get-AdOrganizationalUnit with a filter switch where it checks if OU doesn’t contain any users, Sub OU, computers, or other AD objects.

Get-AdOrganizationUnit in PowerShell retrieves one or more Active Directory organizational units. Filter parameter specifies which organizational unit to retrieve.

In this article, we will discuss how to get empty organizational units in the Active Directory using PowerShell.

Get Empty OrganizationalUnit in AD

The empty organizational unit in Active Directory doesn’t contain any child objects.

Using the Get-AdOrganizationalUnit cmdlet in PowerShell with Filter parameter, we can easily get empty OU in AD. Use the below example

 Get-ADOrganizationalUnit -Filter * | Where-Object {-not ( Get-ADObject -Filter * -SearchBase $_.Distinguishedname -SearchScope OneLevel )} | Select-Object Name,DistinguishedName

In the above PowerShell script to find and list empty OU in AD,

In the first command, the Get-AdOrganizationalUnit gets all OU available in the domain and pipes all OUs to the Get-AdObject cmdlet.

The second command uses a Where-Object clause to check if Get-AdObject gets the child object in the OU specified by a distinguished name.

The Get-AdObject uses the SearchBase parameter to search for child objects in the specified OU.

It uses the SearchScope parameter with OneLevel to get child objects of the parent OU and exclude OU itself.

The Select-Object gets the Name and DistinguishedName of empty OU and prints it on the console.

The output of the above command to list empty OU using PowerShell is

Get-AdOrganizationalUnit Empty OU list
Get-AdOrganizationalUnit Empty OU list

In the above output, FINANCE, HR OU’s in the Active Directory doesn’t contain any users, groups, computers, or AD objects in it. These are the empty OU in Active Directory.

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

Conclusion

I hope the above article on how to find an empty organizational unit in the active directory is helpful to you.

Finding out empty ou in the Active Directory and cleaning up empty OU can be easily done using PowerShell Get-AdOrganizationalUnit.

Cool Tip: How to find computers in OU in the Active Directory!

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