PowerShell Genie

Export OU inventory

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Export Active Directory OUs to CSV with their names, distinguished names, and accidental-deletion protection settings.

The script

1PS> Get-ADOrganizationalUnit -Filter * | Select-Object Name,ProtectedFromAccidentalDeletion,DistinguishedName | Export-Csv .\OUs.csv -NoTypeInformation

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 (No pipeline output. The selected objects were written to the CSV file.)
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-ADOrganizationalUnit -Filter * | Select-Object Name,ProtectedFromAccidentalDeletion,DistinguishedName | Export-Csv .\OUs.csv -NoTypeInformation
    1. Stage 1Get-ADOrganizationalUnit-Filter
    2. Stage 2Select-ObjectPositional values or no named parameters
    3. Stage 3Export-Csv-NoTypeInformation

      How it works

      Get-ADOrganizationalUnit with -Filter * walks every OU in the domain, Select-Object keeps the name with the ProtectedFromAccidentalDeletion flag and the distinguished name, and Export-Csv writes the file with -NoTypeInformation so no type header contaminates the first row.

      Adapting it

      The output path is relative, so the file lands wherever the session happened to be. Give Export-Csv an absolute path, and add a datestamp to the filename if these captures are meant to be compared over time.

      Requirements

      ActiveDirectory module.

      If the result is unexpected

      The OU export is incomplete or has unexpected path columns.

      The search base can exclude part of the directory, and formatting before CSV export discards useful objects.

      1. Confirm SearchBase and select the desired OU properties before exporting the objects.

      Validate the result

      The exported row count matches the intended OU query and paths remain complete.

      Keep the check reliable

      Store the search base with the export and keep presentation formatting out of the data pipeline.

      Primary documentation

      Export-Csv reference

      Continue
      Genie guideExport OU inventory

      Export Active Directory OUs to CSV with their names, distinguished names, and accidental-deletion protection settings.