Beginner question concerning filtering - please help

Dear All,

I was struggling for hours with some code, which just do not compute.

I am trying to pass a variable to this cmdlet

get-mailbox -filter {emailaddresses -like “*”}

so I run following code

$email = “*

Looks the same to me like above command but when I execute it nothing happens

get-mailbox -filter {emailaddresses -like “$email”}

Well this is unfair. Why does it not work? Is there a method to find it out? How to make it work?

I would be deeply grateful, if you would enlighten me :slight_smile:


best regards


artur, welcome to Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

Did you try to search for a solution before you came here to ask? :wink:

These filters are black boxes that no one understands how they really work. It’s trial and error. The type of -filter is actually string , not scriptbloock, so it has to get converted to string first. Maybe try this, but I don’t have that command. Something similar works for get-aduser.

get-mailbox -filter { emailaddresses -like $email }

Or this should work. Don’t ask me why one thing doesn’t work with quotes, and the other requires quotes.

get-mailbox -filter " emailaddresses -like '$email' "

Dear Olaf and js,


Let me express my gratitude for the effort to answer my questions.

I have read Read Me Before Posting! You’ll be Glad You Did!. I assume Olaf that you point to the fact that my code was not preformatted. My bad I was looking this option in the menu and did not realised that it is the option in the Paragraph drop-down list.

Anyhow thank you so much for the link Olaf, this was right direction. js you are also right.

This works:


Get-Mailbox -Filter "EmailAddresses -like '$test'" | FL *LINK*


This does not work:


Get-Mailbox -Filter {EmailAddresses -like '$test'} | FL *LINK*



Get-Mailbox -Filter {EmailAddresses -like "*"} | FL *LINK*

works just fine


Feels bit like WTF, because it feels like this is something which is beyond the rational. Main thing though, I know how to make it work.


Many thanks again guys


I have found two syntaxes to be the most reliable. As mentioned before, do not use script block notation {} for these filters. The online documentation is incorrect in showing examples of doing so. They are strings and are interpreted as strings.

# Works for cases when $test value does not contain ' (single quote)
# This syntax supports sub-expressions
Get-Mailbox -Filter "EmailAddresses -like '$test'"

# Works for all values that I've considered
# Only supports simple variable references
Get-Mailbox -Filter 'EmailAddresses -like $test'

I’d also like to add that it can be confusing especially for those new to powershell who learn variables expand in “” but not ‘’. That’s not the case for this filter. Also you can add many additional conditions.

Get-Mailbox -Filter "name -like '$user' -and IsShared -eq '$false' -and IsMailboxEnabled -eq '$true' -and ModerationEnabled -eq '$false'"

And the powershell rules still apply so you can break it up on the -and operator

Get-Mailbox -Filter "name -like '$user' -and
                  IsShared -eq '$false' -and
           IsMailboxEnabled -eq '$true' -and
          ModerationEnabled -eq '$false'"

AdminOfThings45 and Doug your help and insights I appriciate a lot :slight_smile: