This guide explains how to use the Get-MgUserMessageCount cmdlet in Microsoft Graph PowerShell to count user mailbox messages and export count data for analysis.
The Get-MgUserMessageCount cmdlet is a powerful tool for administrators working with Microsoft Graph PowerShell. It allows you to quickly retrieve the total number of email messages in a user's mailbox. Whether you are tracking mailbox usage, generating reports, or performing audits, this cmdlet simplifies the task by providing a count of the user's messages. In this article, we’ll explore its syntax, practical usage examples, and advanced scenarios to maximize its potential.
Get-MgUserMessageCount -UserId <String>
This example retrieves the total number of messages in the mailbox of a specific user by using their User Principal Name (UPN).
Get-MgUserMessageCount -UserId "jane.doe@yourdomain.com"
You can loop through a list of users (from a CSV file, for example) and log their message count into a report file.
# Import a CSV of user UPNs and get message counts for all users
$users = Import-Csv "C:\UsersList.csv"
foreach ($user in $users) {
$count = Get-MgUserMessageCount -UserId $user.UserPrincipalName
Add-Content -Path "C:\MessageCountReport.txt" -Value "$($user.UserPrincipalName): $count messages"
}
| Error | Cause | Solution |
| Invalid UserId | The UserId provided may be incorrect, or the user does not exist in your tenant. | Double-check the UserId (UPN or user ID) format. You can verify it using the Get-MgUser cmdlet to ensure you are targeting the correct user.Get-MgUserMessage cmdlet to list messages in the user's mailbox and find the required ID. |
| Insufficient Permissions | The account executing the command lacks the required permissions to access the user’s mailbox data. | Ensure the user account has the correct permissions, such as the Mail.ReadBasic or Mail.Read permission in Microsoft Graph. Verify the permissions assigned using Azure AD. |
| Too Many Requests | This error occurs if the script is making too many requests to Microsoft Graph in a short period of time. | Introduce a delay between requests (using Start-Sleep cmdlet) to avoid hitting the request rate limit. Microsoft Graph also supports throttling, so respecting the usage limits ensures smoother execution. |
# Generate a weekly report of user message counts
$users = Get-MgUser -All
foreach ($user in $users) {
$messageCount = Get-MgUserMessageCount -UserId $user.UserPrincipalName
Add-Content -Path "C:\MailboxAudit\WeeklyReport.txt" -Value "$($user.UserPrincipalName): $messageCount messages"
}
# Identify high-volume email users with over 10,000 emails
$users = Get-MgUser -All
foreach ($user in $users) {
$messageCount = Get-MgUserMessageCount -UserId $user.UserPrincipalName
if ($messageCount -gt 10000) {
Write-Host "$($user.UserPrincipalName) has $messageCount emails and may need mailbox optimization."
}
}
What is Get-MgUserMessageCount used for?
Get-MgUserMessageCount is a Microsoft Graph PowerShell cmdlet used to retrieve the total count of messages in a user’s mailbox. It supports filtering to count specific types of messages, such as unread or from a particular sender.
How can I count the total messages in a user’s mailbox?
To count all messages in a user’s mailbox, use the following command:
Get-MgUserMessageCount -UserId "<UserPrincipalName>"
How can I count messages in a specific folder?
You can count messages in a specific folder by including the folder ID. Example:
Get-MgUserMessageCount -UserId "<UserPrincipalName>" -MailFolderId "<FolderId>"
How can I count unread messages?
Use the -Filter parameter to count unread messages:
Get-MgUserMessageCount -UserId "<UserPrincipalName>" -Filter "isRead eq false"
The Get-MgUserMessageCount cmdlet is a simple yet powerful tool for monitoring and managing mailbox usage in Microsoft 365 environments. With just a few lines of PowerShell, administrators can retrieve message counts for individual users or scale up the operation to analyze mailbox usage across the entire organization. By integrating this cmdlet into regular reporting and audit routines, you can optimize email storage and streamline organizational email management.
Take advantage of this cmdlet to improve your email workflows, track user activity, and ensure that your organization’s mailboxes are operating efficiently.
© m365corner.com. All Rights Reserved. Design by HTML Codex