The Get-MgUserMailFolderMessageCount cmdlet is part of the Microsoft Graph PowerShell module, specifically within the Microsoft.Graph.Mail submodule. This cmdlet allows administrators to retrieve the count of messages in a specific mail folder of a user's mailbox. It's particularly useful for monitoring mailbox usage, auditing, and reporting purposes.
Get-MgUserMailFolderMessageCount -UserId <String> -MailFolderId <String> [-Filter <String>] [-Search <String>]
Example 1: Retrieve the Total Message Count in a Specific Folder
$userId = "user@domain.com"
$mailFolderId = "AAMkAGI2TAAA="
Get-MgUserMailFolderMessageCount -UserId $userId -MailFolderId $mailFolderId
This command retrieves the total number of messages in the specified mail folder for the user.
Example 2: Retrieve the Count of Unread Messages in a Specific Folder
$userId = "user@domain.com"
$mailFolderId = "AAMkAGI2TAAA="
Get-MgUserMailFolderMessageCount -UserId $userId -MailFolderId $mailFolderId -Filter "isRead eq false"
This command retrieves the count of unread messages in the specified mail folder by applying a filter.
$userId = "user@domain.com"
Get-MgUserMailFolder -UserId $userId | Select-Object DisplayName, Id
Since the IDs are lengthy, it's advisable to export them for reference:
Get-MgUserMailFolder -UserId $userId | Select-Object DisplayName, Id | Export-Csv -Path "C:\Path\To\MailFolders.csv" -NoTypeInformation
Error | Cause | Solution |
Resource not found | The specified mail folder does not exist. | Verify the MailFolderId parameter using Get-MgUserMailFolder. |
Access Denied | Insufficient permissions to access the user's mail folder. | Ensure the executing account has Mail.Read permissions. |
Invalid UserId | The -UserId parameter is incorrect or the user does not exist. | Double-check the User Principal Name (UPN) or user ID. Use the Get-MgUser cmdlet to verify the user information. |
The Get-MgUserMailFolderMessageCount cmdlet is a valuable tool for administrators to monitor and manage user mailboxes within Microsoft 365. By understanding its syntax, usage, and potential errors, administrators can effectively leverage this cmdlet for various tasks, including auditing, storage management, and user support. Always ensure you have the necessary permissions and verify parameters to avoid common errors.