From this post I learned that I can use [Regex]::Escape() to escape all the commonly escaped characters in string variables. The problem is that I'm going to use a path that contains & characters and then use that in a URL for a RestAPI call, and that escape method does not escape the & character.
When the path contains & the API interprets this as an operator in the URL and is not parsed correctly:
$MyPath = "\\Server\Share\Something & Something Else"
$MyURL= "http://localhost/Address/to/APIResource/"
Invoke-RestMethod -Method Get -Uri ("$MyURL" + "?path=$MyPath") -UseDefaultCredentials
The ?path=$MyPath is the problem, since $MyPath contains & and will therefore not work. How can I escape the & character inside $MyPath to make it work with the RestAPI?