Get-ADGroupmember

Roger Roger 7,181 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 for business | Windows Client for IT Pros | Directory services | Active Directory
Exchange | Exchange Server | Management
Exchange | Hybrid management
Windows for business | Windows Server | User experience | Other
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 51,055 Reputation points MVP Volunteer Moderator
    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. Anonymous
    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.