Inplace of Distingushedname, can i get only OU name in the below powershell command ?
-
Wednesday, January 09, 2013 5:32 PM
Hi
I have below script to pull out the enabled users from Active Directory.
Get-ADUser -Filter {enabled -eq "True"} -SearchBase "DC=child,DC=contoso,DC=org" | Select-Object Name,sAMAccountName,distingushedname
I want to only OU name details and for this what parameter needs to be add.
I know that i can get this through distingushedname but in distingushedname, it porovide full LDAP path.
But I want only OU Name instead of distinguishedname..
- Edited by Mr. Raj Wednesday, January 09, 2013 8:17 PM
All Replies
-
Wednesday, January 09, 2013 6:21 PMModerator
There is no attribute of user objects for the RDN (Relative Distinguished Name) of the parent OU or container of the object. Likewise, there is no parameter of Get-ADUser for this. You must parse the value of distinguishedName (DN), taking into account the possibility that the common name of the user can have an embedded comma.
You could also bind to the user object and invoke the Parent method (exposed by the IADsUser interface), but even this is the DN of the parent OU/container, not the RDN. There are regex expressions for this.
Richard Mueller - MVP Directory Services
- Marked As Answer by IamMredMicrosoft Employee, Owner Friday, January 11, 2013 6:45 PM
-
Wednesday, January 09, 2013 6:30 PMModerator
I found this code, which you can perhaps use:
$DN = "CN=Smith\, James,OU=Sales,OU=West,DC=MyDomain,DC=com"
$Parent = $($DN -split "CN=|,OU=|,DC=")[2]
$Parent
-----
Richard Mueller - MVP Directory Services
- Marked As Answer by IamMredMicrosoft Employee, Owner Friday, January 11, 2013 6:45 PM

