Using filters against objects containing special characters

Another one in the series “blog this so I don’t forget it later”. Here’s the issue: when using server-side filtering (the -Filter parameter, or -RecipientFilter, etc), you have to use the OPATH format and mind some special characters. For example, having an apostrophe in the name of the object.

I run into this when preparing a report on DG membership for some users in specific country. Now, you are probably aware of the quick and easy way to get the “MemberOf” equivalent in EO:

Get-Recipient -Filter {Members -eq 'CN=user,,OU=Microsoft Exchange Hosted Organizations,DC=EURPR03A001,DC=prod,DC=outlook,DC=com'}

This lovely one-liner will save you the trouble of having to enumerate all groups and cycle over them! So it greatly reduces the complexity and time of execution for the script. Note the DN handling however – it needs to be enclosed in single quotation marks. Which coincidentally, are used interchangeably with apostrophes. So in effect, every DN that contains an apostrophe (which is totally legit btw), will cause the nice one-liner above to break. The solution – ‘escape’ the apostrophe in the DN by replacing it with… four (yes, that’s f-o-u-r, 4!) other apostrophes. Or in script form:

$dn = (Get-Mailbox user).DistinguishedName
$dnnew = "'" + "$($dn.Replace("'","''''"))" + "'"
$cmd = "Get-Recipient -Filter 'Members -eq '$dnnew''"
$list = Invoke-Expression $cmd

Now, the filter should work just fine!

10 thoughts on “Using filters against objects containing special characters

  1. Carl says:

    I know this post is a few years old but I was recently dealing with the same issue (DistinguishedName with apostropheses) and found that replacing it with 4 apostrophes didn’t work. Rather, replacing it with just 2 apostrophes did the trick. Code below:

    $dnnew = “’” + “$($dn.Replace(“’”,”””))” + “’”

    Many thanks for getting me on the right track!

  2. Dave Nicholson says:

    Hi, just came across this issue doing exactly the same filter as you. Not sure why you need to use Invoke-Expression in your solution though. I managed to get around this with the following;

    $dn = $($_.DistinguishedName) -replace “‘”,””””
    Get-Recipient -Filter “Members -eq `’$dn`'”

    Much easier to read. Just need to escape the single quotes in the filter so you can use the variable (now adjusted using your ‘replace with 4 single-quotes method)


  3. Victor says:

    I found that they even don’t support -not operator in the filter. 🙁

    Get-EXORecipient -Filter “-not (Name -like ‘mailbox*’)”

    Error message:
    Get-EXORecipient : -not is not a valid operand, please modify your filter.

    1. Vasil Michev says:

      That’s again because of the limitations of the Graph…

      1. Victor says:

        Just wanted to update that since EXO V2 module was GA released on June 3, 2020, the -not operator is supported now. Wow!

  4. Jeremy B says:

    I realize this is old now, but would like to point out that the new EXO V2 cmdlets have officially lost this capability. I’ve reported the issue, and will point it out on

    Get-Recipient -Filter “Name -eq ‘Chips A”Hoy'”

    Doesn’t work:
    Get-EXORecipient -Filter “Name -eq ‘Chips A”Hoy”

    According to here – (and the Get-EXOMailbox page as well, in the -Filter parameter section in either case) – we should be able to use curly brackets to get around this, but that also doesn’t work:

    Doesn’t work either:
    Get-EXORecipient -Filter {Name -eq “Chips A’Hoy”}

    I want to believe / am hoping that this is a problem with how MS has created the cmdlet, and that it’s not truly a problem with OPath filtering syntax. To me, it’s got to be how they’re interpreting the cmdlet input, and ideally a simple regex-style fix could be had. Here’s hoping.

    1. Vasil Michev says:

      Graph has tons of limitations for filters, and lots of special characters need different handling as well. I did report few such issues previously and they fixed them, so just send them an email (

    2. Victor says:

      EXO V2 module was GA released on June 3, 2020. The following seems to be working.

      Get-EXORecipient -Filter “Name -eq ‘Chips A`”Hoy'”
      Get-EXORecipient -Filter “Name -eq ‘Chips A””Hoy'”


Leave a Reply

Your email address will not be published. Required fields are marked *

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