I have some code to retrieve an Azure Active Directory group via the Graph API using the Microsoft.Graph module:
$currentGroup = Get-MgGroup -Filter ('DisplayName eq ' + "'" + $strThisGroupDisplayName + "'")
I originally thought that this and some related code were failing when the group specified by $strThisGroupDisplayName contained a comma. It turns out that this was a miscommunication between me and another team member - a CSV download of groups from the Azure AD portal did not include the comma in the group's displayName for whatever reason. However, the group was created correctly, including the comma in the name.
But that got me thinking: are there any characters that need escaping in filters called using the Microsoft.Graph PowerShell module?
I did some searching and haven't found anything concrete except for this post on escaping a single quote/apostrophe: Single quote escaping in Microsoft Graph, this post on an ampersand: Issue in Matching Department Name while using Microsoft Graph API V1.0, and this post on a hashtag/pound sign: Microsoft Graph filter groups with # in name
I was originally thinking that I might need to replace any single quotes/apostrophes in $strThisGroupDisplayName with a double single quote, e.g.:
$strThisGroupDisplayName = $strThisGroupDisplayName.Replace("'", "''")
Next, perhaps I needed to URL-encode the whole thing? For example:
$strThisGroupDisplayName = System.Web.HttpUtility]::UrlEncode($strThisGroupDisplayName)
However, from the comments below, it seems that maybe none of this is necessary, and the encoding happens automatically in the Microsoft.Graph module?
Edited to add: I did have to escape DisplayNames that contained an apostrophe/single quote - otherwise I received an error. To do this, I used the following code:
$currentGroup = Get-MgGroup -Filter ('DisplayName eq ' + "'" + ($strThisGroupDisplayName.Replace("'", "''")) + "'")
I can also confirm that pound signs/hashtags (#) and commas (,) in the display name did not cause an issue; the Microsoft.Graph module must be encoding these.