5 Useful Scripts with Get-MgGroupMember in PowerShell

Knowing who belongs to a Microsoft 365 group is a daily admin need. Whether you’re auditing access, troubleshooting membership issues, or preparing reports, the Get-MgGroupMember cmdlet in Microsoft Graph PowerShell gives you a fast way to retrieve and analyze group members.

In this article, you’ll learn 5 simple and practical scripts that show different ways to work with Get-MgGroupMember. Each example is beginner-friendly and useful in real admin scenarios.


Prerequisites

Before using these scripts, install and connect Microsoft Graph PowerShell with the right permissions:

Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Group.Read.All","User.Read.All"
                                        

You’ll also need the GroupId of the group you want to query. You can find one using:

Get-MgGroup -Filter "displayName eq 'Group Name'"

  • Script 1: Get Members of a Group
  • Retrieve all members of a specified group.

    Get-MgGroupMember -GroupId "<GroupId>"

    Explanation:

    This is the most basic way to list members. It returns every object inside the group — usually users, but sometimes devices or service principals too. Great for quickly verifying membership.

  • Script 2: Search Members of a Group
  • Search for group members using a specific search string.

    Get-MgGroupMember -GroupId "<GroupId>" -Search "displayName:John" -ConsistencyLevel eventual

    Explanation:

    This script searches within the group’s members.

    • -Search "displayName:John" filters members whose display name matches “John”.
    • -ConsistencyLevel eventual is required when using search in Graph PowerShell.

    Useful when you want to confirm whether a specific person is part of a group.

  • Script 3: Select N Members from group
  • Retrieve the top N members of the group.

    Get-MgGroupMember -GroupId "<GroupId>" -Top 10

    Explanation:

    This retrieves only the first 10 members. It’s handy when testing scripts or when you don’t need the full membership list (especially for large groups).

  • Script 4: Sort Group Members and List Them
  • Sort the group members based on a specific property.

    Get-MgGroupMember -GroupId "<GroupId>" -Sort "displayName asc" -ConsistencyLevel eventual -CountVariable groupCount

    Explanation:

    This script returns members sorted alphabetically by displayName.

    • -Sort "displayName asc" sorts in ascending order.
    • -CountVariable groupCount stores the member count in a variable, which is useful if you want to display or export it later.
    • -ConsistencyLevel eventual is required because sorting uses advanced query features.

    Great for generating clean admin reports.

  • Script 5: Select Group Member User Properties
  • Retrieve the group members and then fetch their displayName and userPrincipalName with the help of Get-MgUser cmdlet:

    # Retrieve members of a specified group
    $groupMembers = Get-MgGroupMember -GroupId "1cbe8c31-589d-453a-a1e5-045f7f00c967"
    # Initialize an array to store detailed user information
    $userDetails = @()
    # Loop through each group member and retrieve additional properties
    foreach ($member in $groupMembers) {
        $user = Get-MgUser -UserId $member.Id -Property "id, displayName, userPrincipalName"
        $userDetails += [PSCustomObject]@{
            Id                 = $user.Id
            DisplayName        = $user.DisplayName
            UserPrincipalName  = $user.UserPrincipalName
        }
    }
    # Display the detailed user information
    $userDetails | Select-Object Id, DisplayName, UserPrincipalName
                                                

    Explanation:

    Get-MgGroupMember usually returns only limited information.

    This script:

    1. Retrieves all members of the group.
    2. Loops through each member.
    3. Uses Get-MgUser to collect friendly properties like DisplayName and UPN.
    4. Outputs a clean, readable list.

    This is one of the most useful real-world reporting patterns for admins.


Cmdlet Tips

  • Get-MgGroupMember may return users, devices, or apps — not only people.
  • Use -Top when testing scripts on large groups.
  • For -Search or -Sort, always include:
    • -ConsistencyLevel eventual
  • If you need more user details, combine with Get-MgUser like Example 5.
  • Exporting results is easy once you have the list:
  • $userDetails | Export-Csv C:\Reports\GroupMembers.csv -NoTypeInformation

Possible Errors and Solutions

Error Cause Solution
Insufficient privileges to complete the operation Missing Graph scopes Reconnect with "Group.Read.All","User.Read.All"
Request_UnsupportedQuery Search/sort without consistency level Add -ConsistencyLevel eventual
Group not found Incorrect GroupId Verify using Get-MgGroup
Cannot index into a null array Empty group Check if group has members or handle null safely

Conclusion

The Get-MgGroupMember cmdlet is a must-know tool for Microsoft 365 administrators. Whether you're listing members, searching within them, sorting results, limiting output for testing, or building detailed reports — these five scripts cover the most practical day-to-day use cases.

Once you're comfortable with these patterns, you can easily extend them into automated audits, compliance checks, or scheduled membership reports.


Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© Your Site Name. All Rights Reserved. Design by HTML Codex