There are 2 ways around this as I see it, there might be an easier way of doing it though.
One is to search for all users which's memberOf attribute has the DistinguishedName of the group (this might be the less cumbersome approach):
$group = 'CN=myGroup, OU=myOU, DC=myDomain' $searcher = [adsisearcher]"(&(objectclass=user)(objectcategory=person)(memberof=$group))" $members = foreach($member in $searcher.FindAll()) { $member.Properties.samaccountname }
The other way around is using the same approach as you're using in your question:
$group = 'CN=myGroup, OU=myOU, DC=myDomain' $adsi = [adsi]"LDAP://$group" $members = foreach($member in $adsi.member) { $isUser = [adsi]"LDAP://$member" if('person' -in $isUser.objectclass) { $isUser.samaccountname } }
Similar as the one above, but using adsisearcher, not sure which one would be more efficient in this case:
$members = foreach($member in $adsi.member) { $check = [adsisearcher]"(&(distinguishedname=$member)(objectclass=user)(objectcategory=person))" if($isUser = $check.FindOne()) { $isUser.Properties.samaccountname } }
omissing fromsamAccuntName