Getting Members of Groups in AD

Hello Everyone,

I am trying to get a list of all members of several groups from within AD, and am really having a hard time wrapping my brain around this. I am also trying really hard to try and figure it out on my own, without stealing…I mean re purposing anyone else’s work that I can find on the internet - I want to LEARN this, but additionally, nothing that I can find will do exactly what I want anyway. The goal here is to product a nice looking HTML-based report using the EnhancedHTML2 Module.

Here is my problem: I have gotten to the point where I can either get the list of groups from a specific OU using

Get-ADGroup -Filter * -SearchBase “OU=Exmaple,DC=domain,DC=DCThings” -Properties name | Select name

Which works, and lists the groups, but if I try to add a Foreach statement below it, such as

$adgroup = get-adgroup -Filter * -SearchBase "OU=OUThings,OU=OtherOUThings,OU=Companies,DC=Domain,DC=local" -Properties name

Foreach ($group in $adgroup){

Get-ADGroupMember $group | select name


All I get is a list of the people who are actually in the group, but the group names disappear.

I did find one solution that used a hash table, but that didn’t present the data in the way I would like. It gave me something along the lines of:

Group1 Name1
Group1 Name2
Group1 Name3
Group2 Name1
Group3 Name1
Group3 Name2
Group3 Name3

Instead, I’m looking for something along the lines of:






What I would like help with, is just direction on how I can accomplish this, I’m not looking for someone to do my work for me and write all of the code (although some examples might help).

Any insight that can be given would really be appreciated.

Thank you in advance.

Hey there Richard,

When you run Select-Object, you’re only asking for Name, which would be the name of the user. Run the command instead as Select-Object -First 1 and then find the properties you want (Name, GroupName, etc) and then use that to construct the list of properties to display.