Get-MgUserMemberOf

What is Get-MgUserMemberOf?

The Get-MgUserMemberOf cmdlet in Microsoft Graph PowerShell retrieves the directory objects (such as groups, directory roles, or administrative units) that a user is a member of. It helps administrators quickly identify user memberships within Microsoft 365.


Why Use Get-MgUserMemberOf?

Membership management is a key part of user administration. With Get-MgUserMemberOf, admins can:

  • Audit group memberships for compliance.
  • Verify user access to security and distribution groups.
  • Export memberships for reporting.
  • Automate membership checks as part of onboarding or offboarding processes.

This cmdlet makes group membership retrieval faster and scriptable, reducing manual checks in the admin center.

Prerequisites

Before running this cmdlet, install the Microsoft Graph module and connect with the appropriate permissions:

Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All"

How to Use Get-MgUserMemberOf?

Syntax (essential parameters):

Get-MgUserMemberOf -UserId <UserPrincipalName or ObjectId>

You can retrieve all memberships, limit the results, or filter them and export for further use.


Get-MgUserMemberOf Examples

  • Retrieve all groups a user is a member of
  • Get-MgUserMemberOf -UserId "john.doe@contoso.com" -All
  • Retrieve a specific number of group memberships
  • Get-MgUserMemberOf -UserId "john.doe@contoso.com" -Top 3
  • Filter and Export Group Memberships to CSV
  • $userId = "john.doe@contoso.com"
    $groups = Get-MgUserMemberOf -UserId $userId -All
    $groups | Export-Csv -Path "C:\UserGroups\SalesTeamGroups.csv" -NoTypeInformation
  • Retrieving Display Name for Each Group
  • Get-MgUserMemberOf returns only object IDs. Use Get-MgGroup with Get-MgUserMemberof to fetch group display names.

    $userId = "samadmin@7xh7fj.onmicrosoft.com"
    # Get the list of objects the user is a member of
    $memberOf = Get-MgUserMemberOf -UserId $userId -All
    # Initialize an array to store the detailed group information
    $detailedGroups = @()
    # Loop through each member object and get additional details
    foreach ($object in $memberOf) {
        $groupId = $object.Id
        try {
            # Get detailed information about the group
            $group = Get-MgGroup -GroupId $groupId -Select DisplayName, Id
            $detailedGroups += $group
        } catch {
            Write-Warning "Could not retrieve details for group with ID: $groupId"
        }
    }
    # Display the detailed group information
    $detailedGroups | Format-Table -Property DisplayName, Id -AutoSize
                                                

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