Skip to content
Menu

Where Did This Email Go - Message Trace

Lists recent mail from Microsoft 365 by sender, recipient and subject, newest first, one row per recipient with its delivery status. Up to ten days back, read-only.

Where did this email go - message trace is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Where did this email go - message trace. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, 11, Server 2016+, with the app certificate; runs as SYSTEM or a user
NeedsPowerShell 5.1; Graph: Organization.Read.All; ExchangeOnlineManagement 3.1+, Exchange.ManageAsApp, message trace role
Read-onlyReads, changes nothing

What It Does

The script connects to Exchange Online as your app and runs a message trace for the last Hours hours, at most 240, which is the ten days one trace query can span. You can narrow it by Sender, Recipient and Subject text, each optional. It reads only and changes nothing.

It writes up to MaxRows results, newest first. A message sent to several recipients can appear in one row per recipient. Each row has the receive time, sender, recipient, subject, delivery status, size and trace ID. The answer counts rows by status and flags when the final result reaches MaxRows.

It uses the newer trace command when the session offers it and falls back to the older one when it does not. The first output line says which ran. With the older command, subject filtering happens after reading the first page. A small or empty result can miss a match beyond that page, so narrow the window or filter on sender or recipient.

It does not start a historical search for mail older than ten days. Trace data can lag delivery by a few minutes, so a message sent a moment ago may not show yet. If the ExchangeOnlineManagement module is missing, it ends SKIPPED.

Before You Run It

  • Install ExchangeOnlineManagement 3.1 or later on the computer that holds the app's certificate.
  • Give the app Organization.Read.All, Exchange.ManageAsApp and an Exchange role that can run message trace, such as Exchange Administrator.
  • Start with sender or recipient plus a short window. A narrow trace is faster and fits inside MaxRows.
  • Wait a few minutes after the mail was sent before you trace it.
  • Use ExportPath or OutputFormat=Csv when you want to send the list to the client.

Type and Risk

  • Type: Report. It collects facts into a table and never alerts. It exits 0, or 2 when it could not run.
  • Risk: Low. It reads and changes nothing.
  • Restart: It never restarts the machine.
  • Typical run: Under 1 minute.
  • Example answer: "Message trace (Get-MessageTraceV2): 12 message(s) in 24 h; Delivered 11, Failed 1"
  • Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.

Inputs

Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.

  • TenantId: The client's tenant ID (a GUID) or domain such as contoso.onmicrosoft.com. Required.
  • ClientId: The app registration's Application (client) ID, a GUID. Required.
  • CertificateThumbprint: Thumbprint of the app's certificate, with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. Leave empty to use the ClientSecret environment variable.
  • Sender: Only messages from this address (for example [user sign-in]). Default empty: any sender.
  • Recipient: Only messages to this address. Default empty: any recipient.
  • Subject: Only messages whose subject contains this text. Default empty: any subject.
  • Hours: How far back to look, in hours. Whole number, 1 to 240. Default 24.
  • MaxRows: The most messages to list. Whole number, 1 to 5000. Default 100.

Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.

License

Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.

The Script

get-message-trace.ps1 · 1028 lines · 56.6 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.