This guide explains how to use the Get-MgUserMemberGroup cmdlet in Microsoft Graph PowerShell to retrieve the groups a user is a member of with practical examples.
The Get-MgUserMemberGroup cmdlet is part of the Microsoft Graph PowerShell module. It is used to retrieve the list of group IDs for groups of which the specified user is a direct member. This cmdlet is crucial for administrators who need to audit group memberships for users in their Microsoft 365 environment.
Install-Module Microsoft.Graph -Scope CurrentUser
Get-MgUserMemberGroup -UserId <String> -BodyParameter <IDirectoryObjectGetMemberGroupsParameter> [<CommonParameters>]
Parameters:
-UserId:
Specifies the unique identifier of the user.-BodyParameter:
Specifies a hash table with the parameter(s) for the request. It can be used to filter groups being retrieved based on the group type.<CommonParameters>:
Supports common parameters like -Verbose -Debug -ErrorAction etc.$userId = "user@example.com"
$params = @{
SecurityEnabledOnly = $false
}
Get-MgUserMemberGroup -UserId $userId -BodyParameter $params
This command retrieves the IDs of all groups (excluding security enabled groups) for which the specified user is a member.
$userId = "user@example.com"
$params = @{
SecurityEnabledOnly = $true
}
Get-MgUserMemberGroup -UserId $userId -BodyParameter $params
This command retrieves only the IDs of security-enabled groups for the specified user.
To get detailed information about the groups you can use the Get-MgGroup cmdlet in conjunction with Get-MgUserMemberGroup.
$userId = "user@example.com"
$params = @{
SecurityEnabledOnly = $false
}
$groupIds = Get-MgUserMemberGroup -UserId $userId -BodyParameter $params
foreach ($groupId in $groupIds) {
Get-MgGroup -GroupId $groupId
}
This script retrieves the detailed information of each group for which the user is a member.
Error | Solution |
Error: "Resource not found" | Ensure the -UserId parameter is correct and corresponds to a valid user in your Microsoft 365 tenant. |
Error: "Insufficient privileges" | Make sure your account has the necessary permissions to retrieve group memberships. You might need the Group.Read.All or Directory.Read.All permissions. |
Cannot bind parameter 'UserId | Verify that the -UserId parameter is not null or empty and is in the correct format (e.g., user@example.com or user ID). |
$Groups = Get-MgUserMemberGroup -UserId "<UserPrincipalName>" -SecurityEnabledOnly $false
$Groups | Export-Csv -Path "C:\Path\To\UserGroups.csv" -NoTypeInformation
Get-MgUserMemberGroup
Outputs a List of Group IDsGet-MgGroup
in a loop or batch query.
-SecurityEnabledOnly $true
-SecurityEnabledOnly $true
to return only security groups the user belongs to.
The Get-MgUserMemberGroup cmdlet is a powerful tool for administrators to manage and audit group memberships in Microsoft 365. By leveraging this cmdlet along with other Microsoft Graph cmdlets like Get-MgGroup and Get-MgUser, you can create detailed reports and automate various administrative tasks efficiently. Understanding its syntax, parameters, and common errors will enhance your ability to maintain a secure and well-organized Microsoft 365 environment.
For further details, refer to the official Microsoft documentation: Get-MgUserMemberGroup cmdlet.
© m365corner.com. All Rights Reserved. Design by HTML Codex