Tip: You can extract the OU (Organizational Unit) from DN (Distinguished Name) in Active Directory.
For example, someone sent you one report with DN only and you need to extract the OU.
1 2 3 4 5 6 7 8 9 10 |
$distinguishedName = 'CN=SMITH John,OU=Managers,OU=EMEA,DC=contoso,DC=com' # Solution 1 [regex]::match($distinguishedName,'(?=OU)(.*\n?)(?<=.)').Value # Solution 2 $distinguishedName.Substring($distinguishedName.IndexOf('OU=')) # Solution 3 (([ADSI]"LDAP://$distinguishedName").parent).Substring(7) |