Get M365 Emails from Specific Sender with Graph PowerShell

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.

The Script: Retrieve Emails from a Specific Sender

# 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

How the Script Works

  • Connect to Microsoft Graph: The script starts by connecting to Microsoft Graph using the Mail.Read permission, which allows the script to access the user’s mailbox and retrieve emails.
  • Identify the User and Sender: The $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.
  • Retrieve Emails from the Sender: The 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.
  • Display the Results: If emails from the specified sender are found, they are displayed in a table format showing the subject and received date. If no emails are found, the script informs you that no matching emails exist in the mailbox.
  • Completion: After processing the emails, the script disconnects from Microsoft Graph, closing the session.

Further Enhancements

  • Export Emails to CSV: You can modify the script to export the list of emails from the sender to a CSV file for reporting or analysis purposes.
  • $emailsFromSender | Export-Csv -Path "C:\Reports\EmailsFromSender.csv" -NoTypeInformation
  • Flag or Move Emails: After retrieving the emails, the script can be enhanced to automatically flag these messages for follow-up or move them to a specific folder for better organization.
  • Add Multiple Filters: You can apply additional filters such as date range or importance to retrieve only high-priority emails from the sender or those received within a certain time frame.
  • $emailsFromSender = Get-MgUserMessage -UserId $UserId -Filter "sender/emailAddress/address eq '$senderEmail' and receivedDateTime ge 2023-10-01"
  • Monitor Multiple Users: The script can be extended to check emails from a specific sender across multiple users in the organization, making it a useful tool for monitoring critical communications.
  • $users = @("user1@domain.com", "user2@domain.com")
    foreach ($user in $users) {
        # Logic for retrieving emails from the sender in each user's mailbox
    }

Possible Errors & Solutions

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.

Conclusion

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!

Suggested Reading

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