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]
Pingback: Don’t do that #11 : Reload a module with Remove-Module and Import-Module - Powershell Guru
Pingback: Don’t do that #13 : Use Write-Host to properly align the output - Powershell Guru