Searching for specific text in emails is a common administrative task. Whether you're investigating email delivery issues, locating specific reports, or assisting users in managing their mailboxes, a targeted search is invaluable. This article provides a PowerShell script using Microsoft Graph to search for a specific string in the body of emails within a user's Inbox.
# Install the Microsoft Graph PowerShell module if not already installed
# Install-Module -Name Microsoft.Graph -Scope CurrentUser
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.Read"
# Specify the User Principal Name (UPN) of the mailbox to query
$UserUPN = "user@yourtenant.onmicrosoft.com"
# Specify the search string to look for in the email body
$SearchString = "Your weekly PIM digest for MSFT"
# Search emails in the inbox containing the specified string
$Inbox = Get-MgUserMailFolder -UserId $UserUPN -Filter "displayName eq 'Inbox'" -Select Id
if (-not $Inbox) {
Write-Output "Inbox not found for $UserUPN."
Disconnect-MgGraph
return
}
$InboxId = $Inbox.Id
# Use the $search parameter to search the email content
$MatchingEmails = Get-MgUserMailFolderMessage -UserId $UserUPN -MailFolderId $InboxId -Search $SearchString -Select "id,subject,from,receivedDateTime"
# Check if any matching emails are found
if ($MatchingEmails) {
Write-Output "Found the following emails containing '$SearchString':"
$EmailDetails = @()
foreach ($email in $MatchingEmails) {
Write-Output "Subject: $($email.Subject)"
Write-Output "From: $($email.From.EmailAddress.Address)"
Write-Output "Received: $($email.ReceivedDateTime)"
Write-Output "------------------------------------"
$EmailDetails += [PSCustomObject]@{
Subject = $email.Subject
Sender = $email.From.EmailAddress.Address
ReceivedDate = $email.ReceivedDateTime
}
}
# Export the details to a CSV file
$ExportPath = "MatchingEmails.csv"
$EmailDetails | Export-Csv -Path $ExportPath -NoTypeInformation
Write-Output "Emails containing '$SearchString' have been exported to: $ExportPath"
} else {
Write-Output "No emails found containing '$SearchString' in the inbox for $UserUPN."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
$AllEmails = Get-MgUserMailFolderMessage -UserId $UserUPN -MailFolderId $InboxId -Select "id,subject,bodyPreview"
$MatchingEmails = $AllEmails | Where-Object { $_.BodyPreview -like "*$SearchString*" }
-Filter "receivedDateTime ge 2024-01-01T00:00:00Z"
Import-Csv "UserList.csv" | ForEach-Object {
$UserUPN = $_.UserPrincipalName
# Insert script logic here
}
Send-MailMessage -To "admin@example.com" -Subject "Search Results for $SearchString" -Body "The report is attached." -Attachments $ExportPath
Error | Cause | Solution |
Access Denied | The account or app lacks Mail.Read permission | Grant the required permissions in Azure AD and ensure admin consent. |
Inbox Not Found | The script cannot locate the Inbox folder | Verify the folder name ("Inbox") and its existence in the user's mailbox. |
No Emails Found | No emails match the search string | Ensure the search string is correct and matches the email content. |
API Throttling | Too many requests sent in a short time | Add a delay between API requests for large mailboxes or multiple users. |
This Graph PowerShell script provides a straightforward way to search for specific strings in the body of emails, helping administrators locate critical communications and support user requests. With options for exporting results and automating tasks, this script is a valuable tool for managing email workflows efficiently. Try it in your environment and tailor it to your specific needs.
© m365corner.com. All Rights Reserved. Design by HTML Codex