Powershell – EMS: Filter Get-Mailbox results to Database with ‘-like’ and wildcards

distribution-listsexchangepowershell

The end-game here is to create dynamic distribution groups based on similar Exchange databases. We have personnel mailboxes split into several databases by region and naming follows set conventions.

As a proof of concept I've been using the "Get-Mailbox" command with a -Filter and -or operators to list multiple databases and it works. The result is something like this:

Get-Mailbox -Filter {
(
(Database -eq 'CN=DBname01,CN=therestoftheDistinguishedName,DC=com') 
-or 
(Database -eq 'CN=DBname02,CN=therestoftheDistinguishedName,DC=com')
)
}

This works fine… I get a list of mailboxes in those databases. But isn't ideal if another database is added due to an increased user count.

MS documentation here says that the "homeMDB/Database property is compatible with wildcards…

So, this should then theoretically work and give the same result:

Get-Mailbox -Filter {(Database -like "CN=DBname*")}

But, this command returns no results…. no errors… nothing.

Is -like just not supported? Am I doing something wrong?

P.S. '-Filter' will be replaced with '-RecipientFilter" for the New-DynamicDistributionGroup command. Get-Mailbox is being used to verify that my filter is working correctly.

Best Answer

Try this command:

get-mailbox |where{$_.database -like 'DBname*'}|ft name,database