Using Get-MgGroupCount in Graph PowerShell

The Get-MgGroupCount cmdlet is part of the Microsoft Graph PowerShell SDK. It allows you to retrieve the total number of Microsoft 365 groups in your tenant or filter the count based on specific properties such as group type or creation date. This cmdlet is helpful for generating quick summaries of groups, aiding in reporting and automating group management tasks.


Cmdlet Syntax

Get-MgGroupCount [-Filter <String>] [-Search <String>] [-ConsistencyLevel <String>]

This cmdlet supports several optional parameters:

  • -Filter: To filter the groups based on specified criteria.
  • -Search: To search groups by specific properties.
  • -ConsistencyLevel: When performing large queries or using -Filter, use the ConsistencyLevel parameter set to Eventual for accurate results.

Usage Examples

1. Retrieve the Total Count of All Groups

This basic example retrieves the total number of Microsoft 365 groups in your tenant.

Get-MgGroupCount -ConsistencyLevel Eventual

This command counts all the groups in the tenant, providing a high-level overview.

2. Retrieve Group Count Based on Filter Criteria

You can filter the groups by specific criteria such as groups created after a certain date or those of a specific type (e.g., unified groups or security groups).

Get-MgGroupCount -Filter "groupTypes/any(c:c eq 'Unified')" -ConsistencyLevel Eventual

This example counts only the groups that are Microsoft 365 (unified) groups. The -Filter parameter allows for precise group queries based on specific group properties.

3. Retrieve Group Count Based on Display Name (Using -Filter)

You can search groups based on keywords such as those whose display names contain certain strings.

Get-MgGroupCount -Filter "startsWith(DisplayName'Le')" -ConsistencyLevel Eventual

This query will return the count of groups where the display name starts with "Le". The startsWith function is an OData query option that works well with the -Filter parameter.


Cmdlet Tips

  • ConsistencyLevel Parameter: For any query that involves filtering or searching, always use -ConsistencyLevel Eventual. This ensures that the query returns consistent and accurate results.
  • OData Filter Expressions: When using the -Filter parameter, remember that the filter expressions follow the OData query format. Review the OData query documentation to understand how to build advanced filters.
  • Search Limitations: The -Search parameter supports only certain properties like DisplayName. Attempting to search with unsupported properties will result in errors.

Possible Errors & Solutions

Error 1: "Property 'xyz' is not found on type 'microsoft.graph.group'"

Cause: This error occurs when you use a non-existent or invalid property with the -Filter parameter.

Solution: Verify the correct group properties by referring to the Microsoft Graph API documentation. You can also retrieve a group's properties by running:

Get-MgGroup -Top 1 | Get-Member

Error 2: "Request_UnsupportedQuery"

Cause: This error occurs when using unsupported operators or properties with the -Filter or -Search parameters.

Solution: Ensure you are using valid OData query operators (like eq, ne, gt, lt) and supported properties. You can reference the OData documentation for proper usage.

Error 3: "ConsistencyLevel is required for this operation."

Cause: When running queries with the -Filter or -Search parameters, omitting the -ConsistencyLevel Eventual parameter causes this error.

Solution: Always include the -ConsistencyLevel Eventual parameter when using filters or searches to ensure proper query execution.


Use Cases

1. Group Auditing and Compliance Reports

A common use case for the Get-MgGroupCount cmdlet is generating reports to track the number of Microsoft 365 groups in the tenant. You can use this cmdlet to monitor the growth of groups over time or filter by specific criteria, such as groups created in the last month, to audit newly created groups.

2. Automating Governance Policies

If your organization has policies that dictate a maximum number of groups, you can automate scripts using Get-MgGroupCount to trigger alerts when group count exceeds a specific threshold. You can also filter groups based on specific properties such as display names or group types to enforce naming conventions and other governance rules.

3. Simplifying Large Tenants' Management

For organizations with a large number of groups, it's crucial to quickly get a summary of group counts without having to retrieve full lists of group objects. Using Get-MgGroupCount, admins can track the number of active groups versus inactive ones or categorize groups by type (e.g., security vs. Microsoft 365 groups) to streamline the management process.


Conclusion

The Get-MgGroupCount cmdlet is a powerful tool for managing and reporting on Microsoft 365 groups. Whether you're generating audit reports, enforcing governance policies, or simplifying management tasks, this cmdlet provides an efficient way to retrieve summary data about your groups. By leveraging filtering, searching, and using OData query expressions, you can tailor your queries to get exactly the information you need. Always remember to include the -ConsistencyLevel Eventual parameter for reliable results and use OData filters carefully to avoid common errors.

Make sure to incorporate this cmdlet into your daily operations to improve efficiency and gain valuable insights into group management within your Microsoft 365 environment.


Additional Resources:

Graph PowerShell Get-MgGroupCount Cmdlet Documentation
Microsoft Graph PowerShell Documentation
Microsoft Graph API Documentation

Related Articles:

Listing Guest Users in a Microsoft Team Using Graph PowerShell
How to Check for Password Expired Users in Microsoft 365
How to Check for Sign-In Enabled Users in Microsoft 365
How to Remove Users from a Microsoft 365 Group Using PowerShell
Counting Groups in Your Microsoft 365 Tenant
How to Retrieve the Last User Sign-In Date Using Graph PowerShell
How to Get a List of Archived Teams in Microsoft 365 Using Graph PowerShell
Monitoring Guest User Invitations in Microsoft 365 Using Graph PowerShell
How to Retrieve Teams Channel Details in Microsoft 365 Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex