Public/Get-OUStructure.ps1
function Get-OUStructure { [CmdletBinding()] Param ( # LDAP path to OU where you wish to start [Parameter(Mandatory = $true, Position = 0)] $RootOU ) Process { $i = 0 function Get-OuRec($baseou) { $i++ $ous = Get-ADOrganizationalUnit -Filter * -SearchScope OneLevel -SearchBase $baseou foreach ($o in $ous) { if ((Get-ADOrganizationalUnit -Filter * -SearchScope OneLevel -SearchBase $o | Measure-Object | Select-Object -ExpandProperty count) -gt 0) { $line = ("`t" * $i) + '-' + $o.name Write-Host $line Get-OuRec($o) } else { $line = ("`t" * $i) + '-' + $o.name Write-Host $line } } } Get-OuRec($rootou) } End { } } |