Flagged emails play a crucial role in helping users stay organized by marking messages that require follow-up or immediate attention. For administrators, being able to monitor these flagged emails in user mailboxes can be vital for tracking high-priority messages and ensuring that important tasks don't fall through the cracks.
In this article, we'll guide you through a PowerShell script that uses Microsoft Graph to retrieve flagged emails from a user's mailbox. This tool is especially useful for administrators who need to manage shared mailboxes or monitor flagged items in key users' mailboxes.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.Read"
# Define the user whose flagged emails you want to retrieve
$UserId = "user@yourdomain.com"
# Retrieve flagged emails from the user's mailbox
$flaggedEmails = Get-MgUserMessage -UserId $UserId -Filter "flag/flagStatus eq 'flagged'" -Property Subject ReceivedDateTime Sender
# Display flagged emails
if ($flaggedEmails.Count -gt 0) {
Write-Host "Flagged Emails for Follow-up in $UserId's Mailbox:"
$flaggedEmails | Select-Object Subject ReceivedDateTime Sender | Format-Table
} else {
Write-Host "No flagged emails found for follow-up in $UserId's mailbox."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Get-MgUserMessage
cmdlet is used to retrieve emails from the user's mailbox that have been flagged for follow-up.$flaggedEmails | Export-Csv -Path "C:\Reports\FlaggedEmailsReport.csv" -NoTypeInformation
$flaggedEmails = Get-MgUserMessage -UserId $UserId -Filter "flag/flagStatus eq 'flagged' and importance eq 'high'"
$users = Import-Csv "C:\UsersList.csv"
foreach ($user in $users) {
$flaggedEmails = Get-MgUserMessage -UserId $user.Email -Filter "flag/flagStatus eq 'flagged'"
# Display or process flagged emails for each user
}
Error | Cause | Solution |
Insufficient privileges to complete the operation. | The connected account does not have the required permission to access the mailbox. | Ensure the account has the Mail.Read permission assigned in Azure AD and that admin consent has been granted. |
Invalid filter clause | The filter syntax might be incorrect or improperly formatted. | Verify that the filter condition uses the correct syntax (flag/flagStatus eq 'flagged' ). |
No flagged emails found | The mailbox may not contain any flagged messages. | Confirm that the user has flagged messages in their mailbox or adjust the filter to match different criteria. |
The term 'Get-MgUserMessage' is not recognized. | The Microsoft Graph PowerShell module might not be installed or updated. | Install or update the Microsoft Graph PowerShell module by running Install-Module Microsoft.Graph . |
Tracking flagged emails using Microsoft Graph PowerShell is a powerful way to manage high-priority tasks and monitor important communications within your Microsoft 365 environment. By automating the retrieval of flagged emails, administrators can quickly identify messages that require attention and ensure that they are handled promptly.
This script provides a solid foundation for monitoring flagged items, and with further enhancements, you can create a robust tool for tracking follow-up tasks in shared or monitored mailboxes. Start implementing this automation today to take control of your organization's email management more efficiently!
© m365corner.com. All Rights Reserved. Design by HTML Codex