Forum Discussion

JuliusPIV's avatar
JuliusPIV
Brass Contributor
Jan 22, 2024

How to Escape Special Characters in the -Filter Property of the Get-IntuneManagedDevice Cmdlet?

TL;DR: 

How do I escape the pound/hash (#) and apostrophe (') characters in the Filter property of the Get-IntuneManagedDevice cmdlet?

 

Full Explanation

I'm leveraging the Get-IntuneManagedDevice cmdlet to get devices associated with users by their UserPrincipalName:

 

Get-intunemanageddevice -Filter "userprincipalname eq 'email userUPN at domain.tld'"

 

 

Whenever I come across a UPN with special characters in it, the cmdlet fails with error:

 

Invalid filter clause: Syntax error at position NN in 'userprincipalname eq 'user D'UPN at domain.tld''.

 

 

So far the I've run into this problem when users have:

  1. "#EXT#" in their UPN indicating an external user
  2. An apostrophe in their name

How do I escape the pound/hash (#) and apostrophe (') characters in the Filter property of the Get-IntuneManagedDevice cmdlet?

 

I considered switching to using the user's object ID, instead of the UPN, but swapping "userPrincipalName" for "Id" or "userId" that doesn't yield valid results and curiously seems to return all devices in the organization.

  • OraDotNetDev's avatar
    OraDotNetDev
    Copper Contributor

    Have you tried using the url escaped values for the filter as follows:
    %27 - quote
    %23 - hash

    -Filter  "not contains(UserPrincipalName, '%23')"

     

     

     

    [uri]::EscapeDataString('''')
    [uri]::EscapeDataString('#')

     

     

     

Resources