Script repository

Output the last time a mailbox received mail

Updated on: Jan 18, 2026, Views: 28616

Exchange

The scripts output the last time when a mailbox received mail. To execute the script, create a business rule, custom command or scheduled task configured for the Group object type.

Exchange on-premises

In the script, the $exchangeServer variable specifies the fully-qualified domain name or IP address of the Exchange server.

$exchangeServer = "exchangeServer.domain.com" # TODO: Modify me

try
{
    # Connect to the Exchange server.
    $session = New-PSSession -connectionURI "http://$exchangeServer/powershell" -ConfigurationName Microsoft.Exchange
    Import-PSSession -session $session -AllowClobber -DisableNameChecking

    # Get the message tracking log for the mailbox.
    $messageTrackingLog = Get-MessageTrackingLog -Recipients "%mail%" -ResultSize Unlimited | Select-Object sender, timestamp | Sort timestamp -Descending
}
finally
{
    # Close connection to the Exchange server.
    Remove-PSSession -Session $session
}

if ($messageTrackingLog -eq $NULL)
{
    $Context.LogMessage("There were no messages sent to this mailbox", "Information")
    return
}

$sender = $messageTrackingLog[0].Sender
$timeStamp = $messageTrackingLog[0].TimeStamp

$Context.LogMessage("Last message received from '$sender' on '$timeStamp'", "Information")

Exchange Online

The script outputs information about the last email received during the previous 10 days. To obtain the same information for a longer period of time, use the ​Start-HistoricalSearch and ​Get-HistoricalSearch cmdlets.

There is no possibility to request emails received by mailboxs in Exchange Online more than 90 days ago.

# Get email address.
$recipientAddress = "%mail%"
if ([System.String]::IsNullOrEmpty($recipientAddress))
{
    if ($Context.TargetObject.RecipientLocation -eq "ADM_EXCHANGERECIPIENTLOCATION_NONE")
    {
        $Context.LogMessage("The object does not have an Exchange mailbox", "Error")
        return
    }

    $mailboxParams = $Context.TargetObject.GetMailParameters()
    $emailAddresses = $mailboxParams.EmailAddresses
    for ($i = 0; $i -lt $emailAddresses.Count; $i++)
    {
        $emailAddress = $emailAddresses.GetAddress($i,[ref]"ADS_PROPERTY_NONE")
        if ($emailAddress.Prefix -eq "smtp" -and $emailAddress.IsPrimary)
        {
            $recipientAddress = $emailAddress.Address
            break
        }
    }
}

$today = Get-Date
$tenDaysAgo = $today.AddDays(-10)

# Connect to Exchange Online.
$Context.CloudServices.ConnectExchangeOnline()

# Get the message trace.
try
{
    $messageTrace = Get-MessageTrace -RecipientAddress $recipientAddress -StartDate $tenDaysAgo -EndDate $today -ErrorAction Stop | Sort Received -Descending
}
catch
{
    $Context.LogMessage("An error occurred while getting messages trace. Error: " + $_.Exception.Message, "Error")
    return
}

if ($NULL -eq $messageTrace)
{
    $Context.LogMessage("There were no messages sent to this mailbox", "Information")
    return
}

$senderAddress = $messageTrace[0].SenderAddress
$received = $messageTrace[0].Received

$Context.LogMessage("Last message received from '$senderAddress' on '$received'", "Information")

Comments 4

You must be signed in to comment.

  • Swapnil joshi

    Swapnil joshi

    How to perform same task in O365

    • Support

      Support

      Hello,

      We added the script for Exchange Online, please, give it a try.

  • Ben

    Ben

    Can we modify or add to this script to generate the last 90 days of usage and export that into a report? We are using O365 so that would be nice to have available. I'd prefer to generate a spreadsheet and see the activity for all of my distribution lists.

    • Support

      Support

      Hello Ben,

      Message trace for more than 10 days cannot be displayed in Adaxes because its generation in Exchange Online takes up to 48 hours and the results can only be sent via email as a CSV file. To receive the report, you can use the Start-HistoricalSearch cmdlet.

Got questions?

Support Questions & Answers

We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.