Format ADSI powershell group.member output

vhabalnortol
vhabalnortol used Ask the Experts™
on
I can't seem to figure out how to extract the member names from AD groups:

This code : [ADSI]$OU2="LDAP://CN=VMpcGroup1,OU=VMSecurityGroups,DC=norton,DC=home,DC=com"
$OU2.member

returns:
CN=VMpcUser1,OU=VMUsers,DC=norton,DC=home,DC=com

How can I just extract the CN, so the result would only be: VMpcUser1?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010
Commented:

There are a few ways to approach that. It's useful to know of them, I tend to use 2 most frequently.

Chris
$Group = "CN=VMpcGroup1,OU=VMSecurityGroups,DC=norton,DC=home,DC=com"

# 1. Parse it out of the string

([ADSI]"LDAP://$Group").member | ForEach-Object { 
  $_ -Replace '(^CN=)|,.*'
}

# 2. Find the members of the group

([ADSISearcher]"(memberOf=$Group)").FindAll() |
  Select-Object @{n='Name';e={ $_.Properties["name"][0] }}

# 3. Get the name from the user object

([ADSI]"LDAP://$Group").member | ForEach-Object { 
  ([ADSI]"LDAP://$_").Name
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial