Jan 22 2024 01:52 PM - edited Jan 23 2024 06:28 AM
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:
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.
Mar 27 2024 08:42 AM - edited Mar 27 2024 08:51 AM
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('#')