Using Get-MgUserCount in Graph PowerShell

The Get-MgUserCount cmdlet is part of the Microsoft Graph PowerShell module and is designed to retrieve the count of user objects in your Microsoft 365 environment. This cmdlet is highly efficient when you want to get insights into the number of users, apply filters, or generate reports without having to pull the full list of users.

In this article, we will cover the syntax, usage examples, cmdlet tips, possible errors with solutions, use cases, and a conclusion to help you leverage Get-MgUserCount for your administrative tasks.

Cmdlet Syntax

Get-MgUserCount [-ConsistencyLevel <String>] [<CommonParameters>]

Key Parameters:

  • -ConsistencyLevel: This parameter is required when applying filters or search queries to ensure eventual consistency. It must be set to 'eventual' for the cmdlet to work.
  • Common Parameters: Supports standard PowerShell common parameters like -Verbose, -Debug, etc.

Usage Examples

Example 1: Get the Total User Count in Microsoft 365

This basic command fetches the total number of users in your tenant.

Get-MgUserCount

Example 2: Get the Count of Disabled Accounts

To count only users who have been disabled, use the accountEnabled property to filter the data.

Get-MgUserCount -ConsistencyLevel eventual -Filter "accountEnabled eq false"

Cmdlet Tips

  • Use Filters Efficiently: Always pair your filtering with the -ConsistencyLevel eventual parameter, or else the cmdlet may return an error.
  • Report Generation: The Get-MgUserCount cmdlet is an efficient way to generate quick reports on user data without retrieving the entire dataset.
  • Performance: Using Get-MgUserCount can significantly reduce the time and resources required compared to using Get-MgUser and counting users manually, especially in large environments.

Possible Errors & Solutions

Error 1: Missing -ConsistencyLevel Parameter

Error: "The request requires the 'ConsistencyLevel' header to be set to 'eventual' for query processing."

Cause: When using filters or complex queries, the -ConsistencyLevel parameter is mandatory.

Solution: Ensure you include -ConsistencyLevel eventual in your query like so:

Get-MgUserCount -ConsistencyLevel eventual -Filter "accountEnabled eq true"

Error 2: Incorrect Filter Syntax

Error: "The property or filter specified is invalid."

Cause: The filter syntax or property used in the query might not be supported or spelled incorrectly.

Solution: Double-check that your filter is using valid properties. Refer to Microsoft’s documentation for correct syntax and supported properties. Example:

Get-MgUserCount -ConsistencyLevel eventual -Filter "assignedLicenses/$count ne 0"

Use Cases

  • Security Audits and Compliance: Organizations often need to conduct audits to ensure that all users meet compliance regulations. By using Get-MgUserCount, administrators can easily count the number of disabled accounts or guest users to ensure adherence to security policies.
  • Get-MgUserCount -ConsistencyLevel eventual -Filter "userType eq 'Guest'"

    This will return the total number of guest users in the environment, which can be crucial for security assessments and compliance checks.

  • License Management: Tracking licensed and unlicensed users helps with optimizing licensing costs. By counting users with and without licenses, organizations can identify gaps and unused licenses.
  • Get-MgUserCount -ConsistencyLevel eventual -Filter "assignedLicenses/$count ne 0"

    This allows you to pinpoint the number of licensed users and compare it against your subscription to make necessary adjustments.

  • Automation of User Management Reports: Many organizations require daily, weekly, or monthly reports on their user base. You can automate these reports using Get-MgUserCount to fetch data like the total number of users or the count of active accounts.
  • 
    $ActiveUsersCount = Get-MgUserCount -ConsistencyLevel eventual -Filter "accountEnabled eq true"
    Send-MailMessage -To "admin@domain.com" -Subject "Daily Active User Count" -Body "The total number of active users is $ActiveUsersCount"
    

Conclusion

The Get-MgUserCount cmdlet is a powerful and efficient tool to get the number of user objects in a Microsoft 365 tenant. Whether you're performing security audits, managing licenses, or automating reports, this cmdlet helps you retrieve essential data quickly. With the proper use of filtering and consistency level settings, you can refine your results and optimize your user management tasks.

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