Yet another solution for grabbing Parent OU paths
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$userArr = Get-ADuser -f * -prop CanonicalName foreach($user in $userArr) { if($matches){$matches.Clear()} $user.CanonicalName -match '.*(?=\/)' | Out-Null $user | Add-Member -MemberType NoteProperty -Name 'custom_ParentCN' -Value $matches[0] -Force if($matches){$matches.Clear()} $user.DistinguishedName -match '(?<=,).*' | Out-Null $user | Add-Member -MemberType NoteProperty -Name 'custom_ParentDN' -Value $matches[0] -Force } $userArr | sort surname | select surname,givenname,custom* | ft -a |