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) |
Pingback: Powershell Tip #125: Identify the duplicates headers of a CSV file | Powershell Guru
Nice,
I recommend the regex with the following upgrade that will work regardless of if the object is in an OU or Container
[regex]::match($distinguishedName,'(?=OU|DC|CN)(.*\n?)(?<=.)').Value
Pingback: Powershell Tip #123: Extract the CN (Common Name) from DN (Distinguished Name) in Active Directory - Powershell Guru