As an administrator, keeping an eye on users’ email activities is critical, especially when troubleshooting or ensuring that important communications are not missed. Whether it's a shared mailbox, a VIP user's inbox, or simply tracking unread messages for compliance purposes, automation can help reduce manual monitoring. With Microsoft Graph PowerShell, you can automate the process of retrieving unread emails for specific users, saving time and effort.
In this article, we'll walk you through a simple yet powerful PowerShell script that leverages Microsoft Graph to retrieve unread emails from any user’s inbox in your Microsoft 365 environment.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.ReadBasic.All"
# Define the user whose unread emails you want to retrieve
$UserId = "user@yourdomain.com"
# Retrieve unread emails from the user's inbox
$unreadEmails = Get-MgUserMessage -UserId $UserId -Filter "isRead eq false" -Property Subject, ReceivedDateTime, Sender | Select-Object Subject, ReceivedDateTime, Sender
# Display unread emails
if ($unreadEmails.Count -gt 0) {
$unreadEmails | Format-Table -Property Subject, ReceivedDateTime, Sender
} else {
Write-Host "No unread emails found for $UserId."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Mail.ReadBasic.All
, which allows the script to retrieve unread emails from users' mailboxes.-Filter
parameter to search only for unread emails (isRead eq false
) returning only specific properties like Subject, ReceivedDateTime, and Sender.While this script is effective in retrieving unread emails, it can be expanded further for additional functionality and flexibility:
$unreadEmails | Export-Csv -Path "C:\Reports\UnreadEmails.csv" -NoTypeInformation
$users = Import-Csv "C:\UsersList.csv"
foreach ($user in $users) {
$unreadEmails = Get-MgUserMessage -UserId $user.UserPrincipalName -Filter "isRead eq false" -Property Subject, ReceivedDateTime, Sender
$unreadEmails | Export-Csv -Path "C:\Reports\UnreadEmails_$($user.UserPrincipalName).csv" -NoTypeInformation
}
$startDate = (Get-Date).AddDays(-7).ToString("yyyy-MM-dd")
$unreadEmails = Get-MgUserMessage -UserId $UserId -Filter "isRead eq false and receivedDateTime ge $startDate"
Get-MgUserMessage
cmdlet to include the message body, attachments, and more for a deeper analysis.Error | Cause | Solution |
Insufficient Privileges to Complete the Operation | The connected account doesn’t have the required API permissions. | Ensure the user or application has the Mail.ReadBasic or Mail.Read permissions. |
The term 'Connect-MgGraph' is not recognized | The Microsoft Graph PowerShell module is not installed. | Install the module using Install-Module Microsoft.Graph . |
Invalid Filter Clause | The OData filter syntax might be incorrect or properties may be case-sensitive. | Double-check the filter syntax ensuring that properties such as isRead are in the correct case and formatted properly. |
With Microsoft Graph PowerShell, administrators can automate various tasks related to email management in Microsoft 365. This simple script to retrieve unread emails for specific users is just one example of how PowerShell and Graph can simplify day-to-day monitoring tasks. It can be further enhanced for reporting, applied to multiple mailboxes, or combined with other scripts for broader automation.
The flexibility of the Graph API and PowerShell makes it a valuable toolset for administrators aiming to streamline processes and boost productivity in managing Microsoft 365 environments.
© m365corner.com. All Rights Reserved. Design by HTML Codex