I have mentioned before that the Charlotte PowerShell User group was frequented by Scripting Guy Ed Wilson, and his wife Teresa. I’m sad to say that they have moved away, but am happy that Brian Wilhite has been running the meetings since. I mentioned to Brian that I had a cool way to get the parent container of an Active Directory object using ADSI:
$U = Get-ADUser $env:username $uParent=(([adsi]"LDAP://$($U.DistinguishedName)").Parent).Substring(7)
Brian’s response was, “why not just use a substring? I came up with this:
$U = Get-ADUser $env:username $uParent =$u.DistinguishedName.Substring($u.DistinguishedName.IndexOf("OU="))
The string method is, of course faster. But If the parent object isn’t an OU, try the first method. It always works.