How to Use Get-MgGroupMember to Fetch Group Members

What is Get-MgGroupMember?

The Get-MgGroupMember cmdlet is part of the Microsoft Graph PowerShell module, which enables administrators to manage Microsoft 365 resources efficiently. This cmdlet is specifically designed to retrieve the members of a given Microsoft 365 group, making it an essential tool for user and group management.

Why Use Get-MgGroupMember?

Administrators often need to check which users belong to a particular group to manage permissions, audit group memberships, or ensure compliance with company policies. The Get-MgGroupMember cmdlet simplifies this process by allowing admins to fetch group members quickly and, when necessary, retrieve additional details about each user.

Cmdlet Syntax

Get-MgGroupMember -GroupId <GroupId> [-All] [-Filter <String>] [-Search <String>]

Key Parameters:

  • -GroupId (Mandatory): Specifies the ID of the Microsoft 365 group.
  • -All: Retrieves all members of the specified group.
  • -Filter: Enables filtering results based on specific conditions.
  • -Search: Performs a search operation within group members.

Usage Examples

  1. Passing Group ID Directly
  2. Get-MgGroupMember -GroupId "1cbe8c31-589d-453a-a1e5-045f7f00c967"

    This command retrieves all members of the specified group.

  3. Passing Group ID When Prompted by Console
  4. Get-MgGroupMember

    If no -GroupId parameter is provided, the console may prompt the user to enter a valid group ID interactively.

  5. Fetch Additional User Information
  6. The following script retrieves detailed user information for each group member:

    # 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
                                                

This script enhances the basic Get-MgGroupMember output by pulling additional user properties using the Get-MgUser cmdlet.


Frequently Asked Questions

  • What is Get-MgGroupMember used for?
    Get-MgGroupMember is a Microsoft Graph PowerShell cmdlet that retrieves members of a specified group in Microsoft 365. It supports filtering by member type (e.g., user, service principal) and exporting results for further analysis.
  • How can I export the list of group members to a CSV file?
    Use the following script to export group members to a CSV file:
    
    $GroupId = "<GroupId>"
    $Members = Get-MgGroupMember -GroupId $GroupId -All
    $Members | Select-Object Id, DisplayName, UserPrincipalName | Export-Csv -Path
  • Can you use Get-MgGroupMember to fetch Group Owners?
    No, you cannot use Get-MgGroupMember to fetch group owners. Get-MgGroupOwner cmdlet is one that fetches group owners.
  • Can you search for group members using Get-MgGroupMember Cmdlet?
    Yes Get-MgGroupMember supports search operation. You have to pass the search string to the -Search parameter
    Get-MgGroupMember -GroupId "" -Search "displayName:John" -ConsistencyLevel eventual
    
  • Why does Get-MgGroupMember return only IDs and not user details?
    By default, it returns minimal data. To include properties like displayName, userPrincipalName, or mail, you need to use the -ExpandProperty parameter or make additional queries using Get-MgUser.

Use Cases

  1. Auditing Group Memberships: Ensure compliance by reviewing users in sensitive security groups.
  2. Automated User Management: Integrate the cmdlet into scripts to automate user role assignments.
  3. Troubleshooting Access Issues: Verify if a user is missing from a group that grants necessary permissions.

👥 Identify Guest Users Using @odata.type in Post-Retrieval Filtering

The Get-MgGroupMember cmdlet returns a mix of directory objects—including users, guests, service principals, devices, or nested groups. After retrieving the members, inspect the @odata.type property to accurately distinguish guest users (e.g., objects with @odata.type equal to #microsoft.graph.user and a userType of Guest).
đź“‘ Use -All or Handle Paging for Complete Results

By default, Get-MgGroupMember returns a limited number of items per call. To retrieve all members—especially in large groups—use the -All parameter or handle @odata.nextLink pagination in a loop to avoid missing entries.
🔢 Count Group Members Easily

To quickly find how many users are in a group, pipe Get-MgGroupMember to Measure-Object:

Get-MgGroupMember -GroupId <group-id> -All | Measure-Object
This returns the total count of group members without needing to export or inspect individual entries.

Conclusion

The Get-MgGroupMember cmdlet is a powerful tool for Microsoft 365 administrators, allowing them to quickly fetch group members and retrieve additional user details when necessary. Whether used for audits, automation, or troubleshooting, this cmdlet simplifies group membership management and enhances administrative efficiency. By leveraging it effectively, admins can ensure a well-maintained and organized Microsoft 365 environment.

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