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 "*"} 
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        :

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 "*"}
C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like ""}
C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like ""}
C:\> Get-Mailbox -Filter {EmailAddresses -like "*"}

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 "*"} | select Name,PrimarySmtpAddress,WindowsEmailAddress

Name                    PrimarySmtpAddress                             WindowsEmailAddress
----                    ------------------                             -------------------

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.

This entry was posted in Exchange Online, Office 365, PowerShell. Bookmark the permalink.

Leave a Reply

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