Retrieve M365 User Flagged Emails Using Graph PowerShell

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.

The Script: Retrieve Flagged Emails in a User's Mailbox

# 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

How the Script Works

  • Connect-MgGraph: The script starts by connecting to Microsoft Graph with the Mail.Read permission, which allows it to access email messages from the specified user’s mailbox.
  • Define the User: It defines the UserId, which refers to the email address of the user whose mailbox you want to monitor for flagged emails.
  • Retrieve Flagged Emails: The Get-MgUserMessage cmdlet is used to retrieve emails from the user's mailbox that have been flagged for follow-up.
  • Display Results: If flagged emails are found, the script displays them in a table format, showing the subject, received date, and sender of each message.
  • Disconnect-MgGraph: Finally, the script disconnects from Microsoft Graph to ensure the session is properly closed.

Further Enhancements

  • Export Flagged Emails to CSV: Modify the script to export the flagged emails to a CSV file for detailed reporting or analysis.
  • $flaggedEmails | Export-Csv -Path "C:\Reports\FlaggedEmailsReport.csv" -NoTypeInformation
  • Include Additional Filters: Enhance the filter criteria to retrieve flagged emails based on other properties such as priority or sender.
  • $flaggedEmails = Get-MgUserMessage -UserId $UserId -Filter "flag/flagStatus eq 'flagged' and importance eq 'high'"
  • Monitor Multiple Users: Use a loop to check flagged emails for multiple users by importing a list of email addresses from a CSV file.
  • $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
    }

Possible Errors & Solutions

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.

Conclusion

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!

Suggested Reading

© m365corner.com. All Rights Reserved. Design by HTML Codex