Get-MgUserMemberOfCount Cmdlet

The Get-MgUserMemberOfCount cmdlet is part of the Microsoft Graph PowerShell module, enabling administrators to count the number of directory objects (such as groups) a user is a member of in Microsoft 365. This cmdlet is particularly useful for managing and auditing user memberships within an organization.

Cmdlet Syntax

Get-MgUserMemberOfCount [-UserId <String>] [-Filter <String>] -ConsistencyLevel eventual 

Parameters

  • -UserId <String>: Specifies the unique identifier or user principal name (UPN) of the user. If omitted, the cmdlet operates in the context of the current user.
  • -Filter <String>: Applies an OData filter to refine the results based on specific criteria.
  • -ConsistencyLevel <eventual>: Sets the consistency level for the query. Acceptable values are Eventual or Session. Using Eventual consistency can improve performance but may return slightly stale data.

Usage Examples

Example 1: Directly Executing Get-MgUserMemberOfCount

Get-MgUserMemberOfCount -ConsistencyLevel Eventual

This command retrieves the count of directory objects the current user is a member of, using eventual consistency for the query.

Example 2: Passing -UserId Parameter

Get-MgUserMemberOfCount -UserId "samadmin@7xh7fj.onmicrosoft.com" -ConsistencyLevel Eventual

This command retrieves the count of directory objects that the user with the UPN samadmin@7xh7fj.onmicrosoft.com is a member of, using eventual consistency.

Example 3: Using -Filter With Get-MgUserMemberOfCount

Get-MgUserMemberOfCount -UserId "samadmin@7xh7fj.onmicrosoft.com" -Filter "groupTypes/any(c:c eq 'Unified')" -ConsistencyLevel Eventual

This command retrieves the count of unified groups that the user samadmin@7xh7fj.onmicrosoft.com is a member of, using a filter to specify unified groups and eventual consistency.


Cmdlet Tips

  • Filtering: Utilize the -Filter parameter to narrow down results based on specific criteria, such as group types or other properties. This can help in obtaining more targeted counts.
  • Consistency Level: The -ConsistencyLevel parameter allows you to choose between Eventual and Session. Eventual consistency offers better performance but may return slightly outdated information, while Session provides more up-to-date data at the cost of performance.
  • User Identification: The -UserId parameter accepts both the user's unique identifier (GUID) and their UPN (e.g., user@domain.com). Ensure the correct format is used to avoid errors.

Possible Errors & Solutions

  • Invalid UserId: If an incorrect or non-existent -UserId is provided, the cmdlet will return an error stating that the user cannot be found. Verify the -UserId value for accuracy.
  • Invalid Filter Syntax: An improperly formatted -Filter parameter can result in syntax errors. Ensure that the OData filter syntax is correct. For example, to filter for unified groups, use: groupTypes/any(c:c eq 'Unified').
  • Permission Issues: Insufficient permissions can lead to authorization errors. Ensure that the executing account has the necessary permissions to access user membership information in Microsoft Graph.

Conclusion

The Get-MgUserMemberOfCount cmdlet is a powerful tool for administrators to efficiently count the memberships of users within an organization. By leveraging parameters like -Filter and -ConsistencyLevel, you can tailor your queries to meet specific requirements. Always ensure that parameter values are accurate and that the executing account has appropriate permissions to avoid common errors.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

© m365corner.com. All Rights Reserved.