Managing emails effectively is a critical task for Microsoft 365 administrators. Sometimes, you need to search for specific emails based on keywords in their subject lines, whether for compliance, audits, or user support. This article introduces a Graph PowerShell script that allows you to search for emails containing specific keywords in their subject and export the results for analysis.
Here’s the complete script to search for emails by a keyword in their subject line and export the results to a CSV file:
# 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 keyword to search for in the email subject
$Keyword = "Important"
# Fetch emails that contain the specified keyword in their subject
$EmailsWithKeyword = Get-MgUserMessage -UserId $UserUPN -Filter "contains(subject, '$Keyword')" -Select "id,subject,from,receivedDateTime"
# Check if any emails are found
if ($EmailsWithKeyword) {
Write-Output "Found the following emails with subject containing '$Keyword':"
foreach ($email in $EmailsWithKeyword) {
Write-Output "Subject: $($email.Subject)"
Write-Output "From: $($email.From.EmailAddress.Address)"
Write-Output "Received: $($email.ReceivedDateTime)"
Write-Output "------------------------------------"
}
# Export the results to a CSV file
$ExportPath = "EmailsWithKeyword.csv"
$EmailsWithKeyword | Select-Object @{Name="Sender";Expression={$_.From.EmailAddress.Address}}, Subject, ReceivedDateTime | Export-Csv -Path $ExportPath -NoTypeInformation
Write-Output "Emails with subject containing '$Keyword' have been exported to: $ExportPath"
} else {
Write-Output "No emails found with subject containing '$Keyword' for $UserUPN."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
$Keywords = @("Important", "Urgent", "Project")
foreach ($Keyword in $Keywords) {
$EmailsWithKeyword = Get-MgUserMessage -UserId $UserUPN -Filter "contains(subject, '$Keyword')" -Select "id,subject,from,receivedDateTime"
# Process each keyword's results
}
-Filter "contains(subject, '$Keyword') and receivedDateTime ge 2024-01-01T00:00:00Z and receivedDateTime le 2024-12-31T23:59:59Z"
Import-Csv "UserList.csv" | ForEach-Object {
$UserUPN = $_.UserPrincipalName
# Insert script logic here
}
Send-MailMessage -To "admin@example.com" -Subject "Email Keyword Report" -Body "Please find the attached report." -Attachments $ExportPath
$EmailsWithKeyword | Select-Object Subject, ReceivedDateTime | Out-File -FilePath "KeywordSearchLog.txt"
Error | Cause | Solution |
Access Denied | The account or application lacks the Mail.Read permission | Grant the required permission in Azure AD and ensure admin consent is provided if necessary. |
No Emails Found | No emails match the specified keyword. | Verify the keyword and ensure that the mailbox contains emails with matching subjects. |
Invalid Filter Clause | Syntax error in the OData filter query. | Confirm the syntax of the contains filter and ensure that it matches the required format. |
API Throttling | Too many requests sent to Microsoft Graph in a short time. | Add a delay between requests when processing multiple mailboxes or large datasets. |
This Graph PowerShell script is a powerful tool for administrators to search for emails based on keywords in their subject. Whether for compliance, audits, or assisting users in locating important messages, this script offers a streamlined solution. By customizing the filters and automating the reporting process, administrators can save time and improve email management workflows.
© m365corner.com. All Rights Reserved. Design by HTML Codex