Home » PowerShell » Find Empty Organizational Unit in Active Directory

Find Empty Organizational Unit in Active Directory

Empty organizational unit in the Active Directory can be retrieved using Get-AdOrganizationalUnit with 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, I will discuss how to get empty organizational units in the Active Directory.

Get Empty OrganizationalUnit in AD

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

Using 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, Get-AdOrganizationalUnit gets all OU available in the domain and pipes all OUs to Get-AdObject cmdlet.

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

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.

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 object in it. These are the empty OU in Active Directory.

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

Conclusion

I hope you may find the above article 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.

Leave a Comment