Exchange Online now supports the -like operator for PrimarySMTPAddress filtering, sort of

In today’s useless bit of information, I present you the news that we can now use the -like operator when filtering against the PrimarySmtpAddress attribute in Exchange Online.

Filters are certainly one of the most useful features and any Exchange (and Office 365) administrator should be taking advantage of them whenever possible. They help you configure your Dynamic groups or address book policies, and they enable you to return just the objects you need, greatly minimizing the time it takes to run reports or perform bulk changes. In the Office 365 world however they come with certain limitations, such as not being able to use leading wildcards or certain type of operators.

For example, if you tried to use a filter based on the PrimarySmtpAddress attribute few years ago and used the -like operator, you would have run into the following error message:

Get-Mailbox -Filter {PrimarySmtpAddress -like "*@michev.info"} 
Property PrimarySmtpAddress does not support Microsoft.Exchange.Data.TextFilter. Only Microsoft.Exchange.Data.ComparisonFilter is supported.
+ CategoryInfo          : NotSpecified: (0:Int32) [Get-Mailbox], ADFilterException
+ FullyQualifiedErrorId : F201E363,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName        : amxprd0310psh.outlook.com

whereas nowadays you can run the same code without any errors. Sadly, without any output either, as the fact that the -like operator is now supported for PrimarySmtpAddress queries, doesn’t mean it actually works 🙂 No matter which variation I try, the end result is zero returned entries, while at the same time filters based on other attributes work just fine:

C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like "*@michev.info"}
C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like "@michev.info"}
C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like "michev.info"}
C:\> Get-Mailbox -Filter {EmailAddresses -like "*@michev.info"}

Name                      Alias           Database                       ProhibitSendQuota    ExternalDirectoryObjectId
----                      -----           --------                       -----------------    -------------------------
bathroom                  bathroom        EURPR03DG246-db075             9.5 GB (10,200,54... 6a9994eb-0449-4f1d-a305-7cf228a06f66
dimo                      dimo            EURPR03DG135-db037             9.5 GB (10,200,54... 34ea864d-8765-45da-8b15-df67cf2fc547

The only type of query that appears to be working is one excluding the domain part, which renders the filter almost useless. For example, I can filter all mailboxes that have primary SMTP addresses in the form vasil*@*, but that’s pretty much the same as filtering on the Name or Alias attribute, and for that we don’t even need filters:

C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like "vasil*"}

Name                      Alias           Database
----                      -----           --------
vasil                     vasil           EURPR03DG245-db082
vasilUS                   vasilUS         EURPR03DG267-db103

C:\> Get-Mailbox vasil*

Name                      Alias           Database
----                      -----           --------
vasil                     vasil           EURPR03DG245-db082
vasilUS                   vasilUS         EURPR03DG267-db103

And what’s even more annoying is that the same type of filters do not work properly on-premises either, even though the example you can find on the -Filter documentation seems to indicate the opposite.

So what does work then? You can certainly do a filter on the EmailAddresses attribute instead. Problem with it is that you cannot use it to match against only the Primary SMTP address, if you include the “SMTP” prefix you end up in a similar situation as with the PrimarySmtpAddress filter. Instead, what you can do, at least in the service, is to use a filter based on the WindowsEmailAddress attribute:

Get-Mailbox -Filter {WindowsEmailAddress -like "*@michev.onmicrosoft.com"} | select Name,PrimarySmtpAddress,WindowsEmailAddress

Name                    PrimarySmtpAddress                             WindowsEmailAddress
----                    ------------------                             -------------------
HuKu                    HuKu@michev.onmicrosoft.com                    HuKu@michev.onmicrosoft.com
NewDiscoveryMailbox     NewDiscoveryMailbox@michev.onmicrosoft.com     NewDiscoveryMailbox@michev.onmicrosoft.com
sharedtest              sharedtest@michev.onmicrosoft.com              sharedtest@michev.onmicrosoft.com

The same trick should work with on-premises Exchange as well, but technically those are two different attributes, so remember to always check whether the PrimarySmtpAddress does match the value of WindowsEmailAddress.

5 thoughts on “Exchange Online now supports the -like operator for PrimarySMTPAddress filtering, sort of

  1. John Smith says:

    Thank you for this. Here’s a snippet to find users whose Primary SMTP Address does not match their “WindowsEmailAddress”. Out of ~2500 objects in my environment I have 8 objects affected so I’m glad I checked.

    Get-RemoteMailbox -ResultSize Unlimited | Where-Object {$_.WindowsEmailAddress -ne $_.PrimarySmtpAddress}

    Reply
  2. Jack Chen says:

    Thanks for the information, WindowsEmailAddress solved my problem.

    Reply
  3. Declan says:

    Because trying to find primary STMP addresses via PowerShell, why would we ever need that to work!

    Reply
  4. Jesper Neumann says:

    Really nice to hear that I’m not the only one that has this problem. Thank you for taking the time to write about it!

    Reply
  5. Spencer Stewart says:

    Oddly I was able to search by the SMTP suffix successfully last week and even used it as a management scope in Exchange Online, but this week I can’t!

    Thanks for writing about this and providing the “WindowsEmailAddress” alternative! Hopefully using the -like operator will work better for the PrimarySmtpAddress attribute in the future 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.