Add Parent OU in DN and CN Path Format Using Regex Match
Yet another solution for grabbing Parent OU paths
$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