Managing mailbox storage efficiently is crucial for Microsoft 365 administrators. Large attachments in emails often consume significant space, and identifying them manually can be time-consuming. With Graph PowerShell, you can automate this process and pinpoint emails with oversized attachments, helping streamline storage management.
In this article, we’ll explore how to use a PowerShell script to retrieve and export emails with large attachments.
Here’s the complete script to retrieve and export emails with attachments exceeding a specified size threshold:
# 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 attachment size threshold (in bytes)
$AttachmentSizeThreshold = 10MB # Example: 10MB = 10 * 1024 * 1024 bytes
# Convert threshold to bytes if specified in MB or KB
$AttachmentSizeThresholdBytes = $AttachmentSizeThreshold * 1024 * 1024
# Query to fetch emails with large attachments
$EmailsWithLargeAttachments = Get-MgUserMessage -UserId $UserUPN -Select "id,subject,from,hasAttachments" -Filter "hasAttachments eq true"
# Check if any emails with attachments are found
if ($EmailsWithLargeAttachments) {
$LargeAttachmentEmails = @()
foreach ($email in $EmailsWithLargeAttachments) {
$Attachments = Get-MgUserMessageAttachment -UserId $UserUPN -MessageId $email.Id
foreach ($attachment in $Attachments) {
if ($attachment.Size -ge $AttachmentSizeThresholdBytes) {
$LargeAttachmentEmails += [PSCustomObject]@{
Subject = $email.Subject
Sender = $email.From.EmailAddress.Address
Size = $attachment.Size
FileName = $attachment.Name
ReceivedDate = $email.ReceivedDateTime
}
}
}
}
# Export large attachment details to a CSV file
if ($LargeAttachmentEmails) {
$ExportPath = "LargeAttachmentEmails.csv"
$LargeAttachmentEmails | Export-Csv -Path $ExportPath -NoTypeInformation
Write-Output "Emails with large attachments have been exported to: $ExportPath"
} else {
Write-Output "No emails with attachments exceeding the size threshold were found."
}
} else {
Write-Output "No emails with attachments were found for $UserUPN."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
-Filter "hasAttachments eq true and importance eq 'high'"
Import-Csv "UserList.csv" | ForEach-Object {
$UserUPN = $_.UserPrincipalName
# Run the script logic for each user
}
Send-MailMessage -To "admin@example.com" -Subject "Large Attachment Report" -Body "The report is ready." -Attachments $ExportPath
This script offers an efficient way to identify and manage emails with large attachments, helping administrators optimize mailbox storage and maintain compliance. By exporting the data to a CSV file, it also allows for further analysis and reporting. With customizable filters and bulk processing options, the script is adaptable for various administrative tasks.
Try this script in your environment and let us know how it simplifies your email management workflows!
© m365corner.com. All Rights Reserved. Design by HTML Codex