This guide explains how to use the Remove-MgUserMessage cmdlet in Microsoft Graph PowerShell to delete email messages from a user’s mailbox. Learn how to remove single or multiple messages with practical examples and best practices.
Managing emails efficiently is a crucial task for IT administrators. The Remove-MgUserMessage cmdlet in Microsoft Graph PowerShell allows you to delete messages from a user's mailbox. This article covers the cmdlet's syntax, usage examples, tips, use cases, possible errors, and their solutions.
Remove-MgUserMessage -UserId <String> -MessageId <String>
Remove-MgUserMessage -UserId "user@domain.com" -MessageId "AAMkADhZD3KZAAA="
Note: Use Get-MgUserMessage cmdlet to get the message id.
$messages = @("AAMkADhZD3KZAAA=", "AAMkADhZD3KZAAQ=")
foreach ($messageId in $messages) {
Remove-MgUserMessage -UserId "user@domain.com" -MessageId $messageId
}
Note: Use Get-MgUserMessage cmdlet to get the message id.
$folderMessages = Get-MgUserMessage -UserId "user@domain.com" -MailFolderId "AQMkADhZD3KZAAAA=" -All
foreach ($message in $folderMessages) {
Remove-MgUserMessage -UserId "user@domain.com" -MessageId $message.Id
}
Note: Use Get-MgUserMailFolder cmdlet to get the mail folder id.
$oldMessages = Get-MgUserMessage -UserId "user@domain.com" -Filter "receivedDateTime lt 2023-01-01"
foreach ($message in $oldMessages) {
Remove-MgUserMessage -UserId "user@domain.com" -MessageId $message.Id
}
Cause: The specified UserId does not exist.
Solution: Verify the UserId and ensure the user exists in the tenant.
Cause: The specified MessageId does not exist.
Solution: Verify the MessageId and ensure the message exists in the user's mailbox.
Cause: Invalid parameters passed to the cmdlet.
Solution: Ensure the UserId and MessageId are correctly formatted and valid.
Cause: Insufficient permissions to delete the message.
Solution: Ensure the executing account has the necessary permissions to delete messages in the specified mailbox. Mail.ReadWrite is the required graph api permission.
1. What is Remove-MgUserMessage used for?
Remove-MgUserMessage is a Microsoft Graph PowerShell cmdlet used to delete email messages from a user’s mailbox, either individually or in bulk.
2. What permissions are required to use Remove-MgUserMessage?
You need the Mail.ReadWrite permission in Microsoft Graph PowerShell. Ensure these permissions are granted in Azure AD.
3. How can I delete all messages from a specific folder?
Use this script to delete all messages from a specific folder:
$Messages = Get-MgUserMailFolderMessage -UserId "" -MailFolderId "" -All
foreach ($Message in $Messages) {
Remove-MgUserMessage -UserId "" -MessageId $Message.Id
}
The Remove-MgUserMessage cmdlet is a powerful tool for managing and cleaning up mailboxes in Microsoft 365. By understanding its syntax, usage examples, and potential errors, IT administrators can efficiently delete unwanted messages, ensuring mailboxes remain organized and secure.
© m365corner.com. All Rights Reserved. Design by HTML Codex