Thursday, October 25, 2018

How to retrieve DN of AD parent object in Powershell

Stumbling over https://www.akaplan.com/blog/2015/09/get-the-parent-ou-for-an-ad-object/ I thought about how to do it correct AND fast:

$U = Get-ADUser $env:username -Properties cn
$uParent =$u.DistinguishedName.Substring( ( $u.cn ).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...)