This guide explains how to use Get-MgUserMessage cmdlet in Microsoft Graph PowerShell to retrieve user emails. Learn to filter messages by properties like sender, subject and export results for analysis.
Get-MgUserMessage cmdlet allows you to retrieve Outlook email messages from a user's mailbox. This article will cover the cmdlet syntax, provide usage examples, offer tips, discuss use cases, highlight possible errors and solutions, and conclude with the benefits of using this cmdlet.
Get-MgUserMessage -UserId <String> [-MessageId <String>]
Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.
Get-MgUserMessage -UserId "john.doe@contoso.com"
This command retrieves all messages for the specified user.
Get-MgUserMessage -UserId "john.doe@contoso.com" -MessageId "AAMkAGI2T..."
This command retrieves a specific message by its message ID.
$User = "samadmin@7xh7fj.onmicrosoft.com"
Get-MgUserMessage -All -UserId "$User" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address }},
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ' ' }} |
Out-GridView
This command retrieves messages with specific properties and displays them in a grid view.
Get-MgUserMessage -UserId "john.doe@contoso.com" -Filter "contains(subject,'Project Update')"
This command retrieves messages that contain "Project Update" in the subject.
Get-MgUserMessage -UserId "john.doe@contoso.com" -Search "Project Update"
This command searches messages that contain "Project Update".
Get-MgUserMessage -UserId "john.doe@contoso.com" -Top 10
This command retrieves the top 10 messages for the specified user.
Get-MgUserMessage -UserId "user@domain.com" -Filter "from/emailAddress/address eq 'ceo@domain.com'" -Top 10
This command fetches the top 10 messages received from ceo@domain.com, using a direct filter on the sender's email address.
| Error | Solution |
| Insufficient privileges to complete the operation. | Ensure the executing user has the required permissions (e.g., Mail.Read) in the Microsoft Graph API. Assign appropriate roles or permissions. |
| Resource not found. | Verify the -UserId and -MessageId parameters. Ensure the user and message exist and are correctly specified. |
| Invalid filter clause | Check the syntax of the -Filter parameter. Ensure it follows the OData filter query format. |
Get-MgUserMessage -UserId "" -Filter "isRead eq false" -All $Messages = Get-MgUserMessage -UserId "" -All $Messages | Select-Object Subject, Sender, ReceivedDateTime | Export-Csv -Path "C:\Path\To\File.csv" -NoTypeInformation
$folder = Get-MgUserMailFolder -UserId "user@domain.com" -Filter "displayName eq 'Inbox'"
$folderId = $folder.Id
# Retrieve messages from the specified folder
Get-MgUserMailFolderMessage -UserId "user@domain.com" -MailFolderId $folderId -All-Property ParameterGet-MgUserMessage returns basic message data.
To improve performance and keep output concise, use the -Property parameter to retrieve only the fields you need — such as:
subjectfromreceivedDateTimehasAttachments-Filter Parameter to Narrow Down ResultsisReadfrom/emailAddress/addressreceivedDateTime"Important" or "Finance") using the -Filter parameter with categories/any().Get-MgUserMessage -UserId user@domain.com -Filter "categories/any(c:c eq 'Finance')"
This is helpful for retrieving categorized emails based on user-defined or automated labels.
The Get-MgUserMessage cmdlet is a powerful tool for administrators to manage and retrieve emails from user mailboxes in Microsoft 365. By leveraging this cmdlet, administrators can automate email-related tasks, generate detailed reports, and troubleshoot email issues efficiently. Proper use of filters, properties, and paging parameters can enhance the performance and relevance of the retrieved data.
By understanding the syntax, usage examples, tips, use cases, and potential errors, administrators can make the most out of the Get-MgUserMessage cmdlet to streamline email management in their organizations.
Note: To interact with user mail via Graph API, use the /users/{id}/messages or /me/messages endpoint. You can filter, sort, or limit fields using OData query options like $filter, $orderby, and $select.
# Replace with the target user's UPN or ID
$userId = "john.doe@yourtenant.onmicrosoft.com"
$response = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/users/$userId/messages"
if ($response.value) {
foreach ($msg in $response.value) {
Write-Output "Subject : $($msg.subject)"
Write-Output "From : $($msg.from.emailAddress.address)"
Write-Output "ID : $($msg.id)"
Write-Output "`n"
}
} else {
Write-Output "No messages found."
}
# Replace with actual UPN and message ID
$userId = "john.doe@yourtenant.onmicrosoft.com"
$messageId = "AAMkADhkZT...f5AAA="
$uri = "https://graph.microsoft.com/v1.0/users/$userId/messages/$messageId"
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
if ($response) {
Write-Output "Subject : $($response.subject)"
Write-Output "Sender : $($response.from.emailAddress.name)"
Write-Output "Body Preview: $($response.bodyPreview)"
} else {
Write-Output "Message not found or access denied."
}
# Retrieve only Subject, Sender, and Received Time
$userId = "john.doe@yourtenant.onmicrosoft.com"
$uri = "https://graph.microsoft.com/v1.0/users/$userId/messages?`$select=subject,from,receivedDateTime"
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
if ($response.value) {
foreach ($msg in $response.value) {
Write-Output "Subject : $($msg.subject)"
Write-Output "From : $($msg.from.emailAddress.address)"
Write-Output "Received : $($msg.receivedDateTime)"
Write-Output "`n"
}
} else {
Write-Output "No messages returned or access denied."
}
# Find messages with subject containing the word 'Invoice'
$userId = "john.doe@yourtenant.onmicrosoft.com"
$filter = "`$filter=contains(subject,'Invoice')"
$uri = "https://graph.microsoft.com/v1.0/users/$userId/messages?$filter"
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
if ($response.value) {
foreach ($msg in $response.value) {
Write-Output "Subject : $($msg.subject)"
Write-Output "ID : $($msg.id)"
Write-Output "`n"
}
} else {
Write-Output "No messages matched the subject filter."
}
💡 Other supported filters include isRead, importance, receivedDateTime, and more.
You’ll need one of the following delegated or app permissions:
Mail.ReadMail.ReadWrite© m365corner.com. All Rights Reserved. Design by HTML Codex