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=RENARD Steve,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

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

  1. Pingback: Don’t do that #11 : Reload a module with Remove-Module and Import-Module - Powershell Guru

  2. Pingback: Don’t do that #13 : Use Write-Host to properly align the output - Powershell Guru

Leave a Reply

Your email address will not be published. Required fields are marked *