list members of multiple AD groups in multiple domains

Ron dorel 21 Reputation points
2022-09-20T18:14:27.837+00:00

Hi All,

I have 20 AD groups of 15 different domains that I need to export their members list.

To clarify this:

Domain#1: V1.Contoso.com Groups: Group1, Group2, Group3 etc

Domain#2: V2.Contoso.com Groups: Group3, Group4, Group5 etc

Domain#3 V3.Contoso.com Groups: Group6, Group7, Group8 etc

Output csv file will provide a separate row for each group member as follows:

Group name,SamAccountName ,Display Name ,description

I'll just add I can do it one by one using for exapmple : get-adgroupmember "Group1" -Server V1.Contoso.com, now looking for a way to save time by doing it for all the groups in one script.
what i had in mind is to put all the group names + server names in a txt file example : Group1,V1.Contoso.com and find a way to use it in a script.

What is the best way to do it ?

Many Thanks!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-09-20T19:00:18.83+00:00

    Hi @Ron dorel ,

    maybe this helps to get started (not tested).

    CSV file content:

    groupname,servername  
    group1,v1.contoso.com  
    group2,v1.contoso.com  
    group3,v2.contoso.com  
    group4,v2.contoso.com  
    

    Script:

    Import-Csv -Path "Junk\GroupsAndServers.csv" | ForEach-Object {  
        get-adgroupmember $_.groupname -Server $_.servername  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


7 additional answers

Sort by: Most helpful
  1. Ron dorel 21 Reputation points
    2022-09-21T10:10:20.85+00:00

    Hi Andreas,
    Thanks a lot indeed ! You saved me a lot of time.
    I tested it and it worked perfectly, as i'm quite new to PowerShell, do you know how can i add to the output file a column of the group name? That is needed because the file is containing multiple groups members , so adding the group name will make it easier to identify which user belongs to each group. Again, many thanks!
    Ron

    0 comments No comments

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-09-21T12:20:11.813+00:00

    Hi @Ron dorel ,

    please try this to get the output of domain, groupname and members (SamAccountName of each member) in a CSV file:

    $inputFile = "Junk\GroupsAndServers.csv"  
    $outputFile = "Junk\full-GroupsAndServers.csv"  
    Out-File -FilePath $outputFile -Encoding utf8 -InputObject "domain,groupname,members"  
    Import-Csv -Path $inputFile | ForEach-Object {  
        $groupname = $_.groupname  
        $servername = $_.servername  
        $membersArray = Get-ADGroupMember $groupname -Server $servername  
        $memberList = ""  
        foreach ($member in $membersArray) {  
            $memberList += $(($member.SamAccountname) + ",")  
        }  
        $memberList = $memberList.Substring(0, $memberList.Length - 1)  
        $servername + ";" + $groupname + ";" + $memberList | Out-File -FilePath $outputFile -Append -Encoding utf8  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Ron dorel 21 Reputation points
    2022-09-21T13:47:39.507+00:00

    Hi @Andreas Baumgarten

    Many thanks for the assistance!
    I tested this script and what i noticed is that the users list is displayed horizontally and not vertically , also, in the first column (domain) in addition to domain name and group name also displayed one of usernames in the group (happened the same for all the groups).
    what i had in mind is the same output format of Get-ADGroupMember + additional column of group name, hope this is possible in some way.
    Regards,
    Ron

    0 comments No comments

  4. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-09-22T06:13:53.207+00:00

    Hi @Ron dorel ,

    here is a different approach:

    $inputFile = "Junk\GroupsAndServers.csv"  
    $outputFile = "Junk\full-GroupsAndServers.csv"  
    $fullList = @()  
    Import-Csv -Path $inputFile | ForEach-Object {  
        $groupname = $_.groupname  
        $servername = $_.servername  
        Get-ADGroupMember $groupname -Server $servername | ForEach-Object {  
            $member = [PSCustomObject]@{  
                Domain            = $servername  
                Group             = $groupname  
                SamAccountName    = $_.SamAccountname  
                distinguishedName = $_.distinguishedName  
                objectClass       = $_.objectClass  
                SIS               = $_.SID  
            }  
            $fullList += $member  
        }  
    }  
    $fullList  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.