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.
Membership management is a key part of user administration. With Get-MgUserMemberOf, admins can:
This cmdlet makes group membership retrieval faster and scriptable, reducing manual checks in the admin center.
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"
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 -UserId "john.doe@contoso.com" -All
Get-MgUserMemberOf -UserId "john.doe@contoso.com" -Top 3
$userId = "john.doe@contoso.com"
$groups = Get-MgUserMemberOf -UserId $userId -All
$groups | Export-Csv -Path "C:\UserGroups\SalesTeamGroups.csv" -NoTypeInformation
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