Fetch Emails Delivered Before a Specific Date Using Graph PowerShell

Managing email efficiently is crucial for IT administrators. One common task is retrieving user emails delivered before a certain date. This article provides a comprehensive PowerShell script using Microsoft Graph to accomplish this task, an explanation of the script, suggestions for further enhancements, and troubleshooting tips.


Script

$User = "samadmin@7xh7fj.onmicrosoft.com"
$Date = "2024-01-01"

Get-MgUserMessage -All -UserId "$User" -Filter "ReceivedDateTime lt $Date" |
    Select-Object Subject, InternetMessageId, ReceivedDateTime, @{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } }, @{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ' ' }} |
    Out-GridView

Script Output


Script Explanation

  • Variables Declaration:
    • $User: The User Principal Name (UPN) of the user whose emails you want to fetch.
    • $Date: The date in YYYY-MM-DD format. The script will fetch emails received before this date.
  • Fetching User Messages:
    • Get-MgUserMessage -All -UserId "$User" -Filter "ReceivedDateTime lt $Date": This cmdlet fetches all user messages where the ReceivedDateTime is less than the specified date.
  • Selecting Specific Properties:
    • Select-Object: This cmdlet is used to select specific properties of the emails such as Subject, InternetMessageId, ReceivedDateTime, Sender, and Recipients.
    • @{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address }}: This expression extracts the sender's email address.
    • @{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ' ' }}: This expression concatenates all recipients' email addresses into a single string separated by spaces.
  • Displaying Output:
    • Out-GridView: This cmdlet displays the output in an interactive grid view window, allowing for easy data inspection and filtering.

Further Enhancements

Export to CSV

To save the fetched emails to a CSV file for further analysis, replace the Out-GridView cmdlet with Export-Csv.

$User = "samadmin@7xh7fj.onmicrosoft.com"
$Date = "2024-01-01"

Get-MgUserMessage -All -UserId "$User" -Filter "ReceivedDateTime lt $Date" |
    Select-Object Subject, InternetMessageId, ReceivedDateTime, @{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } }, @{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ' ' }} |
    Export-Csv -Path "UserEmails.csv" -NoTypeInformation

Adding Additional Filters

To further filter emails based on other properties like IsRead, Importance, etc., you can enhance the filter parameter.

$User = "samadmin@7xh7fj.onmicrosoft.com"
$Date = "2024-01-01"
$Importance = "High"

Get-MgUserMessage -All -UserId "$User" -Filter "ReceivedDateTime lt $Date and Importance eq '$Importance'" |
    Select-Object Subject, InternetMessageId, ReceivedDateTime, @{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } }, @{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ' ' }} |
    Out-GridView

Automating the Script

Schedule the script to run periodically using Task Scheduler to automate the fetching process.


Possible Errors and Solutions

Error: "Get-MgUserMessage : The term 'Get-MgUserMessage' is not recognized as the name of a cmdlet, function, script file, or operable program."

Cause: The Microsoft Graph module is not installed or imported.

Solution: Install and import the Microsoft Graph module.

Install-Module -Name Microsoft.Graph -Scope CurrentUser
Import-Module Microsoft.Graph

Error: "Select-Object : Property 'Sender' cannot be found."

Cause: Incorrect property name or the user has no emails.

Solution: Ensure the property exists and there are emails for the specified date range.

Get-MgUserMessage -All -UserId "$User" -Filter "ReceivedDateTime lt $Date" | Get-Member

Error: "Out-GridView : The term 'Out-GridView' is not recognized as the name of a cmdlet."

Cause: The Out-GridView cmdlet is part of the Microsoft.PowerShell.Utility module, which might not be loaded.

Solution: Ensure the module is imported.

Import-Module Microsoft.PowerShell.Utility

Conclusion

Fetching user emails delivered before a specific date using Graph PowerShell is an efficient way to manage and analyze email data. This script provides a foundation that can be easily extended with additional filters and automation. By understanding and addressing potential errors, administrators can ensure a smooth and reliable execution.

This article has walked you through a practical example, offered enhancements, and provided troubleshooting tips to handle common issues. Using these tools, you can optimize your email management tasks and streamline your IT operations.


Additional Resources:

Microsoft Graph PowerShell Module Documentation
Microsoft Graph API Documentation

Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

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