Exchange PowerShell – Evaluating expressions in the Filter parameter

Recently I needed to get a bunch of groups from Exchange by their ExternalDirectoryObjectId property. There are many ways to do this and one of the easiest would be to just get the Name (or other unique identifier) by looking up the group in AAD via MSOL PS. However, you can use the -Filter parameter for Get-DistributionGroup cmdlet and specify the id:

Get-DistributionGroup -Filter { ExternalDirectoryObjectId -eq $group.ExternalDirectoryObjectId }

However, when you try this you may get an error saying that it’s invalid syntax:

Cannot bind parameter ‘Filter’ to the target. Exception setting “Filter”: “Invalid filter syntax. For a description of
the filter parameter syntax see the command help.
“ExternalDirectoryObjectId -eq $group.ExternalDirectoryObjectId” at position 31.”

After a bit of searching I came across this forum post. Apparently PowerShell won’t evaluate expressions in the filter script block. You can assign the $group.ExternalDirectoryObjectId to it’s own variable before the filter but for me that didn’t work either. What did work was using quotes instead of curly brackets, as below:

Get-DistributionGroup -Filter "ExternalDirectoryObjectId -eq '$($group.ExternalDirectoryObjectId)'"

This finally returns the group by id!

Leave a Reply

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