Monitoring communications from specific individuals or departments is an important task for administrators who manage large mailboxes in an organization. Whether it’s tracking emails from key stakeholders, clients, or specific departments, having a quick way to retrieve emails from a particular sender can help ensure timely responses and proper email management.
Using Microsoft Graph PowerShell, administrators can automate the process of retrieving emails from specific senders in a user’s mailbox. This article provides a script that allows you to filter and retrieve emails from any sender, making mailbox management easier and more efficient.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.Read"
# Define the user and the sender's email address to filter messages
$UserId = "user@yourdomain.com"
$senderEmail = "sender@example.com"
# Retrieve emails from the specified sender in the user's mailbox
$emailsFromSender = Get-MgUserMessage -UserId $UserId -Filter "sender/emailAddress/address eq '$senderEmail'" -Property Subject ReceivedDateTime
# Display the retrieved emails
if ($emailsFromSender.Count -gt 0) {
Write-Host "Emails from $senderEmail in $UserId's Mailbox:"
$emailsFromSender | Select-Object Subject ReceivedDateTime | Format-Table
} else {
Write-Host "No emails from $senderEmail found in $UserId's mailbox."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
$UserId
variable specifies the user whose mailbox will be queried, and $senderEmail
is the email address of the sender whose emails you want to retrieve.Get-MgUserMessage
cmdlet is used to retrieve all emails in the user’s mailbox that match the sender’s email address. The filter sender/emailAddress/address eq '$senderEmail'
ensures that only emails from the specified sender are returned.$emailsFromSender | Export-Csv -Path "C:\Reports\EmailsFromSender.csv" -NoTypeInformation
$emailsFromSender = Get-MgUserMessage -UserId $UserId -Filter "sender/emailAddress/address eq '$senderEmail' and receivedDateTime ge 2023-10-01"
$users = @("user1@domain.com", "user2@domain.com")
foreach ($user in $users) {
# Logic for retrieving emails from the sender in each user's mailbox
}
Error | Cause | Solution |
No emails from the specified sender | The sender might not have sent any emails to the user’s mailbox, or the email address might be incorrect. | Verify that the sender’s email address is correct and check that emails from the sender exist in the mailbox. |
Insufficient privileges to complete the operation | The connected account does not have the necessary permissions to read mailbox content. | Ensure the account has been granted the Mail.Read permission in Azure AD and that admin consent has been provided if needed. |
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 |
Invalid filter clause | The filter syntax might be incorrect or improperly formatted. | Double-check the filter syntax and ensure that the email address is correctly formatted in the query. |
Retrieving emails from a specific sender using Microsoft Graph PowerShell is an effective way to monitor key communications within an organization. By filtering emails based on the sender’s address, administrators can quickly identify important messages and ensure they are addressed.
With further enhancements, this script can be customized to handle additional email management tasks, such as flagging, moving, or exporting emails. Automating the process of retrieving and managing emails from specific senders helps improve communication oversight and ensures that important emails are not overlooked.
Start implementing this script today to streamline mailbox management and monitor critical communications in your Microsoft 365 environment!
© m365corner.com. All Rights Reserved. Design by HTML Codex