Get-mailbox -Filter with variable doesn't work

Can someone explain why this works:

$UserName = “someuser”

Get-ADUser -filter {SamAccountName -eq $Username}

but this doesn’t:

Get-Mailbox -filter {SamAccountName -eq $Username}

A bug in the Exchange cmdlets maybe?

Running Exchange 2010 SP3.

All -Filter parameters pass along their value to the underlying technology. In other words, every -Filter parameter will work entirely differently, based on the command and the underlying technology.

Exchange’s commands don’t use the same filtering concepts as the AD commands. It isn’t a bug, it’s a design thing. That isn’t to say it wouldn’t be more convenient if they were all the same… but they’re not. Because the commands are produced by entirely different product teams, who rarely collaborate, those differences should be expected.

In Exchange’s case, the team didn’t implement -Filter parameters. Instead, they provide other ways for you to specify the mailbox you want. I don’t think any of those relate to samAccountName, though. Keep in mind that a mailbox <> an AD user, although there can obviously be a connection between the two.

If you do Get-Command Get-ADUser -Syntax and Get-Command Get-Mailbox -Syntax , you’ll find that these filter parameters are actually taking strings, not script blocks. Script blocks can convert back to strings, so you don’t get an error, but each cmdlet has its own implementation for what to do with these filter strings. The AD cmdlets do have some logic to look for $variable syntax and try to expand the values, but it still runs into problems in some cases, such as { SamAccountName = $someObject.UserName }. Based on your post, I would assume that the Exchange cmdlets aren’t even going that far; they just don’t work at all.

You can fix this by using double-quoted strings and expanding the variables yourself. Be sure to include quotation marks in the right places, such as:

"SamAccountName -eq '$Username'"

This way, what you’re actually sending to the cmdlets is something like “SamAccountName -eq ‘Dave’” , and there’s no way they can really screw that up. :slight_smile:

Unfortunately, practically every example I see for these commands uses the curly braces, which can be misleading. The only command I know of which actually takes a script block for filtering, off the top of my head, is Where-Object.

As far as I know, Get-Mailbox with a SamAccountName filter should work. I’m basing this on documentation, though; I can’t test it at the moment:

Thanks for the reply. Based on your observations and info, I did some more tests:

This has been working for years (with curly braces to enclone the filter syntax):

Get-Mailbox -filter {samaccountname -eq "jdoe01"}

But replacing “jdoe01” with a variable did not.

I followed your advice to use a variable in the filter syntax and this line worked:

get-mailbox -filter "samaccountname -eq '$Username'"

So, the Get-Mailbox cmdlet doesn’t like the curly braces as much as Get-ADUser :wink:

Learned a little something with this one.

Thanks again.