Tech Community Live: Microsoft Intune
Mar 20 2024, 07:30 AM - 11:30 AM (PDT)
Microsoft Tech Community

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

Brass Contributor

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.

0 Replies