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=tenant.onmicrosoft.com,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!

This entry was posted in Exchange Online, Office 365, PowerShell. Bookmark the permalink.

3 Responses to Using filters against objects containing special characters

  1. Pingback: Using filters against objects containing special characters with Invoke-Command | Blog

  2. 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 techcommunity.microsoft.com.

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

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

    According to here – https://docs.microsoft.com/en-us/powershell/module/exchange/powershell-v2-module/get-exorecipient?view=exchange-ps (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.

    • 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 (exocmdletpreview@service.microsoft.com)

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.