Managing spam and phishing emails is a critical task for Microsoft 365 administrators. By analyzing emails flagged as spam or moved to the Junk Email folder, administrators can monitor email security, troubleshoot false positives, and ensure a clean mailbox environment. This article provides a Graph PowerShell script to retrieve and export emails from the Junk Email folder, along with detailed guidance and enhancements for practical use.
Below is the complete script to retrieve and export emails from the Junk Email folder:
# 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"
# Fetch the Junk Email folder ID
$JunkFolder = Get-MgUserMailFolder -UserId $UserUPN -Filter "displayName eq 'Junk Email'" -Select Id
if (-not $JunkFolder) {
Write-Output "Junk Email folder not found in $UserUPN's mailbox."
Disconnect-MgGraph
return
}
$JunkFolderId = $JunkFolder.Id
# Fetch all emails from the Junk Email folder
$JunkEmails = Get-MgUserMailFolderMessage -UserId $UserUPN -MailFolderId $JunkFolderId -Select "id,subject,from,receivedDateTime"
# Check if any emails are found
if ($JunkEmails) {
Write-Output "Found the following emails in the Junk Email folder:"
foreach ($email in $JunkEmails) {
Write-Output "Subject: $($email.Subject)"
Write-Output "From: $($email.From.EmailAddress.Address)"
Write-Output "Received: $($email.ReceivedDateTime)"
Write-Output "------------------------------------"
}
# Export the emails to a CSV file
$ExportPath = "JunkEmails.csv"
$JunkEmails | Select-Object @{Name="Sender";Expression={$_.From.EmailAddress.Address}}, Subject, ReceivedDateTime | Export-Csv -Path $ExportPath -NoTypeInformation
Write-Output "Emails from the Junk folder have been exported to: $ExportPath"
} else {
Write-Output "No emails found in the Junk Email folder for $UserUPN."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
-Filter "receivedDateTime ge 2024-01-01T00:00:00Z"
-Select "id,subject,from,receivedDateTime,importance,isRead"
Import-Csv "UserList.csv" | ForEach-Object {
$UserUPN = $_.UserPrincipalName
# Insert the script logic here
}
}
Send-MailMessage -To "security@example.com" -Subject "Junk Email Report" -Body "The report is attached." -Attachments $ExportPath
$JunkEmails | Select-Object Subject, ReceivedDateTime | Out-File -FilePath "JunkEmailLog.txt"
Error | Cause | Solution |
Access Denied | Insufficient permissions to access the mailbox. | Ensure the Mail.Read permission is granted in Azure AD for the account.. |
Junk Email Folder Not Found | The Junk Email folder has been renamed or doesn’t exist. | Verify the folder name in the mailbox and adjust the script accordingly. |
No Emails Found | The Junk Email folder is empty. | Check the mailbox settings to ensure emails are routed to the Junk Email folder. |
Throttling Limits Exceeded | Too many requests sent in a short time. | Add a delay between processing multiple mailboxes to avoid throttling. |
This Graph PowerShell script provides an efficient way to retrieve and export emails from the Junk Email folder in a user’s mailbox. By automating the process and exporting the results, administrators can monitor spam activity, investigate false positives, and ensure email security. With options for filtering and bulk processing, this script is a valuable tool for managing email content across an organization.
© m365corner.com. All Rights Reserved. Design by HTML Codex