Thursday, October 25, 2018

How to retrieve DN of AD parent object in Powershell

Stumbling over I thought about how to do it correct AND fast:

$U = Get-ADUser $env:username -Properties cn
$uParent =$u.DistinguishedName.Substring( ( $ ).Length + 4 )

This way we can use the fast string method and we will always remove up to the correct position. Regardless of the parent object container type (OU, Container, whatever...)