Export Azure Security Groups with it's Member details

Prince Chauhan 0 Reputation points
2024-07-17T11:22:07.48+00:00

Hi MSFT community,

Hope you all are doing well!!

we are looking for a script through which we can export our all the Azure security groups and his members details. we tried several scripts but failed to achieve the goal.

please find the screenshot as an example how we want to export the data.

Request you all to help us to resolve this.png2pdf.pdf

Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 49,640 Reputation points MVP Volunteer Moderator
    2024-07-17T11:27:47.3066667+00:00

    try the following:

    Install-Module -Name Microsoft.Graph -Scope CurrentUser
    Connect-MgGraph -Scopes "Group.Read.All", "User.Read.All"
    # Define the output file
    $outputFile = "AzureADGroupsMembers.csv"
    # Get all security groups
    $groups = Get-MgGroup -Filter "securityEnabled eq true and mailEnabled eq false" -All
    # Initialize an array to hold the results
    $groupMembers = @()
    # Loop through each group and get its members
    foreach ($group in $groups) {
        $members = Get-MgGroupMember -GroupId $group.Id -All
        foreach ($member in $members) {
            $groupMembers += [PSCustomObject]@{
                GroupName     = $group.DisplayName
                GroupId       = $group.Id
                MemberName    = $member.DisplayName
                MemberType    = $member.ODataType
                MemberId      = $member.Id
            }
        }
    }
    # Export the results to a CSV file
    $groupMembers | Export-Csv -Path $outputFile -NoTypeInformation
    
    
    

    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

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.