Searching Message Tracking Logs by Sender or Recipient Email Address

Continuing my series of tips on searching message tracking logs using PowerShell, in this article I will demonstrate a few techniques for searching logs based on sender or recipient email address.

The Get-MessageTrackingLog cmdlet provides two parameters for specifying sender and recipient email addresses as search criteria.

  • -Sender – a single SMTP address for the sender of the email message
  • -Recipients – one or more SMTP addresses for the recipients of the email message

Both parameters are optional, so if they are omitted the search will return all senders, all recipients, or all of both.

To demonstrate the use of these parameters consider the following email message sent from Alan Reid to three recipients.

Searching Message Tracking Logs by Sender Email Address

Because I happen to have sent this test message within the last hour it is not very difficult for me to search for by combining the -Sender parameter with the -Start parameter to search within a time/date range.

[PS] C:\>Get-MessageTrackingLog -Sender Alan.Reid@exchangeserverpro.net -Start (Get-Date).AddHours(-1)

EventId  Source   Sender                            Recipients                        MessageSubject
-------  ------   ------                            ----------                        --------------
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Payroll report for September
RECEIVE  SMTP     Alan.Reid@exchangeserverpro.net   {David.Gower@exchangeserverpro... Payroll report for September
DELIVER  STORE... Alan.Reid@exchangeserverpro.net   {Alex.Heyne@exchangeserverpro.... Payroll report for September
DELIVER  STORE... Alan.Reid@exchangeserverpro.net   {David.Gower@exchangeserverpro... Payroll report for September

However, if I were searching over a broader time range I may see more results than I really want to see.

[PS] C:\>Get-MessageTrackingLog -Sender Alan.Reid@exchangeserverpro.net

EventId  Source   Sender                            Recipients                        MessageSubject
-------  ------   ------                            ----------                        --------------
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Descry turmoil deviance
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Impending abeyance recitals ba...
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Egress
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Presage visceral penurious
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Stipple voluble blatant stymie
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Inured
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Heinous mercurial
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Relapse smolder
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Meeting minutes
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Supine poignant
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Indigence denigrate swerve vig...
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Jocular
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Oblivious apropos condone savant
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Obdurate splice penitent
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Extenuate aplomb obtain eulogy
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Cursory cryptic rescind euphoria
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Lucubrate ruffian
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Indigence umbrage
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Emaciate valiant tractable
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Volatile fission cajole
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Concord legacy chisel fagged
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Egress reconcile contrite cred...
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Abstruse salacious constrict
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Unearth recreancy paucity
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                A meeting #1
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                A meeting #2
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Assuage foppish
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Clamor austere collusion
SUBMIT   STORE... Alan.Reid@exchangeserverpro.net   {}                                Waffle saturnine

...snip!

So in the case where I want to search a broader time window, but see fewer irrelevant results, I can combine the -Sender and -Recipients parameters in my search command.

Searching Message Tracking Logs by Recipient Email Address

It doesn’t matter whether the recipient was in the To, CC, or BCC of the message, the search will return any match regardless. Here the “Payroll report for September” email shown above is found even though Alex Heyne was one of several recipients and was in the CC field.

[PS] C:\>Get-MessageTrackingLog -Sender Alan.Reid@exchangeserverpro.net -Recipients alex.heyne@exchangeserverpro.net

EventId  Source   Sender                            Recipients                        MessageSubject
-------  ------   ------                            ----------                        --------------
RECEIVE  SMTP     Alan.Reid@exchangeserverpro.net   {David.Gower@exchangeserverpro... Payroll report for September
DELIVER  STORE... Alan.Reid@exchangeserverpro.net   {Alex.Heyne@exchangeserverpro.... Payroll report for September

You can specify multiple recipient SMTP addresses simply by separating them with a comma. When you do this the condition is an “or” not an “and”. In other words, any messages with any one of the recipients will be returned in the results, they do not need to be messages sent to all the recipients.

Here both the payroll email sent to Alex and David, as well as another email sent only to David, are returned in the same results.

[PS] C:\>Get-MessageTrackingLog -Sender Alan.Reid@exchangeserverpro.net -Recipients alex.heyne@exchangeserverpro.net,david.gower@exchangeserverpro.net

EventId  Source   Sender                            Recipients                        MessageSubject
-------  ------   ------                            ----------                        --------------
RECEIVE  SMTP     Alan.Reid@exchangeserverpro.net   {David.Gower@exchangeserverpro... Payroll report for September
DELIVER  STORE... Alan.Reid@exchangeserverpro.net   {Alex.Heyne@exchangeserverpro.... Payroll report for September
DELIVER  STORE... Alan.Reid@exchangeserverpro.net   {David.Gower@exchangeserverpro... Payroll report for September
RECEIVE  SMTP     Alan.Reid@exchangeserverpro.net   {David.Gower@exchangeserverpro... Also how about lunch?
DELIVER  STORE... Alan.Reid@exchangeserverpro.net   {David.Gower@exchangeserverpro... Also how about lunch?

Searching Message Tracking Logs for Wildcard Values or Partial Matches

Unfortunately wildcard searches are not allowed with the -Sender and -Recipient parameters.

For example, this will return no results.

[PS] C:\>Get-MessageTrackingLog -Recipients *@gmail.com

However, you can use wildcards if you pipe the output of Get-MessageTrackingLog into Where-Object instead.

In this situation it is wise to limit the search to a specific date range for better performance. Or, if you do need to search the entire set of log files remember to use “-Resultsize Unlimited”.

[PS] C:\>Get-MessageTrackingLog -Start (Get-Date).AddHours(-1) | Where-Object {$_.recipients -like "*@gmail.com"}

EventId  Source   Sender                            Recipients                        MessageSubject
-------  ------   ------                            ----------                        --------------
RECEIVE  STORE... Alan.Reid@exchangeserverpro.net   {exchangeserverpro@gmail.com}     Email to the internet!
TRANSFER ROUTING  Alan.Reid@exchangeserverpro.net   {exchangeserverpro@gmail.com}     Email to the internet!
SEND     SMTP     Alan.Reid@exchangeserverpro.net   {exchangeserverpro@gmail.com}     Email to the internet!

You can see that the wildcard is used with the -like comparison operator, but another technique is to use the -match comparison operator which doesn’t require the wildcard character.

[PS] C:\>Get-MessageTrackingLog -Start (Get-Date).AddHours(-1) | Where-Object {$_.recipients -match "gmail"}

EventId  Source   Sender                            Recipients                        MessageSubject
-------  ------   ------                            ----------                        --------------
RECEIVE  STORE... Alan.Reid@exchangeserverpro.net   {exchangeserverpro@gmail.com}     Email to the internet!
TRANSFER ROUTING  Alan.Reid@exchangeserverpro.net   {exchangeserverpro@gmail.com}     Email to the internet!
SEND     SMTP     Alan.Reid@exchangeserverpro.net   {exchangeserverpro@gmail.com}     Email to the internet!

The same use of Where-Object with -like or -match also applies to the sender email address.

Summary

As you can see the -Sender and -Recipients parameters give us some flexibility when searching message tracking logs. However in some cases we need to use the more powerful capabilities of Where-Object for wildcard and partial string matching.

About Paul Cunningham

Paul is a Microsoft Exchange Server MVP and publisher of Exchange Server Pro. He also holds several Microsoft certifications including for Exchange Server 2007, 2010 and 2013. Connect with Paul on Twitter and Google+.

Comments

  1. U. P. B. Michael says:

    Thanks paul – nice article…

  2. I’m looking for a way to count the outgoing mails per user from “day-one” of our mailserver.

    $msgs = get-messagetrackinglog -Sender “username@domain.nl” -EventID “SEND” -Start “1-1-2000 0:00:00″
    $msgs | Group-Object -Property Sender | Select-Object name,count | sort >C:\NumberOutgoingMailsUsername.txt

    The number of mails I get in the output file (156x) is much lower than the message in the users Send items (8488 mails). What am I overseeing? Is this affected by the retention policy?

  3. I dont think it would show BCC information in the message tracking logs?

  4. I’m trying to export to CSV mails sent out from a specific email using commands below:

    get-messagetrackinglog -Sender “email@domain.com” -EventID “SEND” -Start “4/5/2014 8:00:00 AM” -End “4/5/2014 12:00:00 PM” | Where-Object {$_.recipients -like “*gmail.com”} | Export-csv C:\export.csv

    However, in the Recipients field is showing only System.String[] . How can I make it show real recipients email address.

  5. Naga Krishna says:

    Hi Paul,

    Good Evening..!!

    I have situation where the user ( xyz@abc.com) was transitioned to extermal organisation ( xyz@efg.com) . However, He is still continuing to receive emails from our internal users.

    We do not have objects for him either on exchange or AD. Early, Assumptions are that a DL might be involved with a object for him.

    How do we track the emails from internal users to this invisible object in our environent. Thanks

Leave a Comment

*

We are an Authorized DigiCert™ SSL Partner.