Don’t do that #12: Use several lines to get the parent container path of an AD user object

By | April 10, 2015

Don’t do that: The following code gets the parent container path of an AD user object.

Example: CN=Powershell Test,OU=Users,OU=Department,DC=domain,DC=com

$dn = (Get-ADUser -Identity $user -Properties DistinguishedName).DistinguishedName
$split = $dn -split '(?<![\\]),'
$split[1..$($split.Count-1)] -join ','


Do that: It can be done with a easier one-liner:

$parent = $dn.split(',',2)[1]


previous-buttonnext-button

Leave a Reply

Your email address will not be published.