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.
Get-MgGroupCount [-Filter <String>] [-Search <String>] [-ConsistencyLevel <String>]
This cmdlet supports several optional parameters:
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
© m365corner.com. All Rights Reserved. Design by HTML Codex