skip to Main Content

Get Organizational Units with PowerShell

We like to get a list of all the Organizational Units (OUs) in Active Directory and export it with PowerShell. Why do we need that? We like to clean up not used OUs. In this article, you will learn how to display and export a list of Organizational Units with PowerShell.

Get a list of all Organizational Units with PowerShell

Run PowerShell as administrator. Get a list of all the OUs in Active Directory. We will make use of the Get-ADOrganizationalUnit cmdlet. Let’s sort on CanonicalName, this will show us an OU breakdown structure and is easier to read.

The output with all the OUs in AD is a good list. But how do we know if there are users present in the OU?

Get a list of all Organizational Units with UserCount with PowerShell

We like to get a list of the OUs including user count with PowerShell. This will show us if there are users present in the OU. Copy and paste the below code.

It will show an output with UserCount. If the UserCount is showing 0, it means that there are no users in the OU. Note: it will not show if there is a computer object in the OU. This will only check and show a count for users.

Export OUs in AD to a text file or CSV file with PowerShell

Now that we have the list of OUs in AD shown, we like to export it to a file. The script will get the Organizational Units with PowerShell and export it to a text file.

If you like to export to a CSV file, change the last line to $results | Export-Csv -Path C:\export_OUs.csv -NoTypeInformation –Encoding UTF8

After running the above command, find the exported file in the C:\ drive. I opened the text file export_OUs.txt.

Get Organizational Units (ous) with PowerShell text output

Conclusion

To sum it up, you did learn how to get Organizational Units with PowerShell. Not only that, but you also learned how to find the empty OUs. As of last, you learned how to export OUs to a text file or CSV file with PowerShell. Did you enjoy this article? If so, you may like Hide mail-enabled security group from GAL with PowerShell. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect and IT Consultant. His specialism is designing and building complex enterprise environments. He started Information Technology at a very young age, and his goal is to teach and inspire others. Connect with ALI TAJRAN on social media. Read more »

This Post Has One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top