Get-MailboxContent.ps1


Description

Purpose

Retrieves email messages from a specified mailbox.

Detailed Description

This script connects to a specified mailbox and retrieves email messages based on the provided filters.

Back to Top

Usage

Example 1

Get-MailboxContent -Mailbox "[email protected]" -SenderAddress "[email protected]" -Subject "Project Update" -StartReceivedDate "2024-01-01" -EndReceivedDate "2024-01-31" -Verbose

Back to Top

Notes

No additional notes.

Back to Top


Script

<#
.SYNOPSIS
    Retrieves email messages from a specified mailbox.

.DESCRIPTION
    This script connects to a specified mailbox and retrieves email messages based on the provided filters.

.PARAMETER Mailbox
    The email address of the mailbox to retrieve messages from.

.PARAMETER SenderAddress
    The email address of the sender to filter messages by.

.PARAMETER RecipientAddress
    The email address of the recipient to filter messages by.

.PARAMETER Subject
    The subject to filter messages by.

.PARAMETER StartReceivedDate
    The start date to filter messages by.

.PARAMETER EndReceivedDate
    The end date to filter messages by.

.PARAMETER Credential
    The credentials to use for authentication.

.EXAMPLE
    Get-MailboxContent -Mailbox "[email protected]" -SenderAddress "[email protected]" -Subject "Project Update" -StartReceivedDate "2024-01-01" -EndReceivedDate "2024-01-31" -Verbose
#>

function Get-MailboxContent {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]$Mailbox,

        [Parameter(Mandatory = $false)]
        [string]$SenderAddress,

        [Parameter(Mandatory = $false)]
        [string]$RecipientAddress,

        [Parameter(Mandatory = $false)]
        [string]$Subject,

        [Parameter(Mandatory = $false)]
        [datetime]$StartReceivedDate,

        [Parameter(Mandatory = $false)]
        [datetime]$EndReceivedDate
    )

    begin {
        Write-Verbose "Connecting to Microsoft Graph..."
        if (-not (Get-MgContext)) {
            Connect-MgGraph -Scopes "Mail.Read"
        }
    }

    process {
        # Build OData filter string for Get-MgUserMessage
        $filter = @()
        if ($SenderAddress) { $filter += "from/emailAddress/address eq '$SenderAddress'" }
        if ($RecipientAddress) { $filter += "toRecipients/any(t: t/emailAddress/address eq '$RecipientAddress')" }
        if ($Subject) { $filter += "contains(subject,'$Subject')" }
        if ($StartReceivedDate) { $filter += "receivedDateTime ge $($StartReceivedDate.ToString('yyyy-MM-ddTHH:mm:ssZ'))" }
        if ($EndReceivedDate) { $filter += "receivedDateTime le $($EndReceivedDate.ToString('yyyy-MM-ddTHH:mm:ssZ'))" }

        $searchFilter = $filter -join " and "
        Write-Verbose "Search filter constructed: $searchFilter"

        try {
            $params = @{ UserId = $Mailbox; Top = 50; }
            if ($searchFilter) { $params['Filter'] = $searchFilter }
            $messages = Get-MgUserMessage @params
        }
        catch {
            Write-Error "Failed to retrieve messages: $_"
            return
        }

        $result = @()
        foreach ($email in $messages) {
            $result += [PSCustomObject]@{
                Date    = $email.ReceivedDateTime
                From    = $email.From.EmailAddress.Address
                To      = ($email.ToRecipients | ForEach-Object { $_.EmailAddress.Address }) -join ", "
                Subject = $email.Subject
                Body    = $email.Body.Content
            }
        }
        $result
    }
}

# Example usage (uncomment the following line to use the function directly):
# Get-MailboxContent -Mailbox "[email protected]" -SenderAddress "[email protected]" -Subject "Project Update" -StartReceivedDate "2024-01-01" -EndReceivedDate "2024-01-31" -Verbose

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top