Get-ADGroupmember

Roger Roger 6,406 Reputation points
2024-11-02T04:39:49.0633333+00:00

Hi All,

In Active Directory, we have security groups, mail-enabled security groups, and distribution groups. I'm not sure if I'm missing any other group types. I would like to export all users from these groups, including those in nested groups, to a CSV file. Could someone please help me with a PowerShell script to export all the members?

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,810 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,530 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,682 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,691 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
2,147 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 26,620 Reputation points MVP
    2024-11-02T11:29:48.96+00:00

    Try the following (ensure you have the Active Directory module for PowerShell installed)

    # Define the path to save the CSV file
    $outputFile = "C:\ExportedUsers.csv"
    
    # Get all groups by filtering for security groups, mail-enabled security groups, and distribution groups
    $groups = Get-ADGroup -Filter {GroupCategory -eq 'Security' -or GroupCategory -eq 'Distribution'}
    
    # Initialize an empty array to store results
    $results = @()
    
    # Iterate over each group
    foreach ($group in $groups) {
        # Get all members of the group, including those in nested groups
        $members = Get-ADGroupMember -Identity $group -Recursive | Where-Object { $_.objectClass -eq 'user' }
        
        # Add each member's details to the results array
        foreach ($member in $members) {
            $results += [pscustomobject]@{
                GroupName = $group.Name
                UserName = $member.SamAccountName
                UserDisplayName = $member.Name
                UserEmail = $member.EmailAddress
            }
        }
    }
    
    # Export results to a CSV file
    $results | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8
    
    Write-Output "Export completed: $outputFile"
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ian Xue 38,046 Reputation points Microsoft Vendor
    2024-11-04T05:48:18.42+00:00

    Hi,

    You can use the -Recursive parameter to list the member users in the nested groups.

    $groups = @("group1","group2","group3")
    $groups | ForEach-Object {Get-ADGroupMember  -Identity $_ -Recursive} | Export-Csv -Path C:\Folder\GroupMember.csv -NoTypeInformation
    

    $groups is an array of your group names.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    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.