Exchange Online PowerShell – Wildcard Search in Cmdlets

exchangemicrosoft-office-365powershellwindows

Are wildcards supported in Exchange Online PowerShell cmdlets? I want to create a new Address List which filters by Mail address but cannot get it to work.

The following command yields no results even though there are users with such a mail address:

Get-Recipient -Filter {(Alias -ne $null) -and (PrimarySmtpAddress -like '*@domain.com')}

When I type in the whole mail address by hand it returns a result, so it is clear that the wildcard filter does not work.

So are wildcards not supported? If that is the case, then why does the following command return results when filtering mail contacts?

Get-Recipient -Filter {(Alias- ne $null) -and (ExternalEmailAddress -like '*@domain.com')}

Best Answer

Part of the problem that you may be running into is that some attributes are not usable with the -Filter parameter of the Get-Recipient command. For example:

Get-Mailbox -Filter {(Alias -ne $null) -and (ExternalEmailAddress -like '*@example.com')}

Results in:

Cannot bind parameter 'Filter' to the target. Exception setting "Filter": ""ExternalEmailAddress" is not a recognized filterable property. For a complete list of filterable properties see the command help.

One way you can get around this takes longer to process, but works: get all recipients that have an alias, and then use the Where function to process the 2nd part of your query. The resulting command set would look like this:

(Get-Recipient -Filter {(Alias -ne $null)} -ResultSize Unlimited).Where{$_.EmailAddresses -like "*example.com"}

Using the above Where method gives you the same results as piping to the Where-Object cmdlet, but is faster. The following syntax will provide the same results:

Get-Recipient -Filter {(Alias -ne $null)} -ResultSize Unlimited | Where-Object {$_.EmailAddresses -like "*example.com"}