Hi everyone;

I need your help for a request in powershell.

I have a list of a few AD Groups, and i need for each group, to retrieve the members.

The result i get from the cmd i used is not the the one i was expecting. It just displays all the users in those groups, without specifying which user is member of which group.

Please can you tell me what is missing in my command ?

$groups = Get-ADGroup -Filter * -searchBase ‘OU=SRV,OU=Accounts,DC=test,DC=NET’ -Properties name,description | where {$_.name -like ‘buil*’} | select name

$ADgroupmember = $groups | ForEach-Object {Get-ADGroupMember $_.name} | select samaccountname

$ADgroupmember | ForEach-Object {Get-ADUser $_.samaccountname | select name }

Here is the result:

Name

John
freddy_t2
Gandalf_t2
Marvin
Linda
John
Linda_t2
Marvin
John
Linda_t2
freddy_t2

Big thanks in advance for your precious help.

11 Spice ups

Here’s one way to do it.

$groups = Get-ADGroup -Filter * -searchBase 'OU=SRV,OU=Accounts,DC=test,DC=NET' -Properties name,description | where {$_.name -like 'buil*'} | select name

foreach($group in $groups){Get-ADGroupMember $group.name | select @{name='groupname'; expression={$group.name}},samaccountname,name}

Get-ADGroup has a ‘members’ property.
So as long as members aren’t bested you can get them all in one swoop

Hi, thaks a lot for your reply @jayparker9836

I got a better result now; Big thanks

Groupname samaccountname


buildbaseimage John
buildbaseimage freddy_t2
buildbaseimage Gandalf_t2
buildfpm jy816
buildfpm Marvin
buildfpm Linda
buildfpm John
buildfpm Linda_t2
buildpxp Marvin
buildmtsnom Marvin
buildsmss John
buildspt Linda_t2
buildmona freddy_t2

Is it normal we see the same group more than once on different lines ? I thought it was possible to get the group name only once, and then the members.

It is pretty simple to code when you are querying a single group.

I like having the group name beside each line, then I can filter the results in excel on a username and see if they have multiple group memberships.

Yes it should make it easier to manage in this way. Anyway your reply helped me a lot.

1 Spice up

No problem. Happy to help. Another member might post something else, hard to say for sure if they will or not.

At some point though, if there are no other posts, don’t forget to come back and I’ll ask that you mark a post as Best Answer so this question is marked as [SOLVED] here, in Spiceworks search results, and in internet search results. Thanks :+1:

https://community.spiceworks.com/how_to/126499-how-to-give-out-best-answers-in-the-spiceworks-commun… .