While working with URLs in PowerShell, dealing with special characters in URLs is challenging. One such character that often causes headaches is the ampersand (&). In this tutorial, I will explain how to escape ampersands in URLs with PowerShell with examples.
To escape ampersands in URLs using PowerShell, you can utilize the [System.Web.HttpUtility]::UrlEncode method to encode the parameter containing the ampersand. For example, if you have a URL parameter like company=Smith&Co, you can encode it as follows: $encodedParam = [System.Web.HttpUtility]::UrlEncode("Smith&Co"), resulting in Smith%26Co. This ensures the ampersand is correctly interpreted in the URL, preventing parsing errors.
Escape Ampersands in URLs with PowerShell
The ampersand is a reserved URL character often used to separate query parameters. When an ampersand appears in a URL parameter value, it can cause the URL to be parsed incorrectly, leading to errors. For example, if you’re sending a GET request to a web service with a URL that includes an ampersand, you need to ensure the ampersand is properly escaped to avoid issues.
Basic Syntax for Escaping Ampersands
In PowerShell, the escape character is the backtick (\“). However, when dealing with URLs, the ampersand should be encoded as %26`. This ensures that web servers correctly interpret the URL.
Example 1: Basic URL Encoding
Let’s start with a simple example. Suppose you want to send a GET request to a web service with a URL that includes an ampersand in one of the parameters.
$baseUrl = "https://api.example.com/data"
$param1 = "John"
$param2 = "Doe&Co"
$encodedParam2 = [System.Web.HttpUtility]::UrlEncode($param2)
$url = "$baseUrl?firstName=$param1&company=$encodedParam2"
$url
Invoke-WebRequest -Uri $urlIn this example, we use [System.Web.HttpUtility]::UrlEncode to encode the company parameter, ensuring the ampersand is escaped as %26. This method is straightforward and leverages .NET’s built-in URL encoding functionality.
Here is the output in the screenshot below:

Check out PowerShell Variables in Quotes
Example 2: Using Invoke-RestMethod
Another common scenario is using Invoke-RestMethod to interact with RESTful APIs. The process is similar, but let’s add more complexity by including multiple parameters.
$baseUrl = "https://api.example.com/data"
$params = @{
firstName = "Jane"
lastName = "Smith"
company = "Smith&Associates"
}
# Encode each parameter
foreach ($key in $params.Keys) {
$params[$key] = [System.Web.HttpUtility]::UrlEncode($params[$key])
}
# Construct the query string
$queryString = ($params.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join "&"
$url = "$baseUrl?$queryString"
Invoke-RestMethod -Uri $urlHere, we encode each parameter individually and then construct the query string. This approach is useful when you have multiple parameters and want to ensure each one is correctly encoded.
Check out How to Escape Dollar Signs in PowerShell?
Example 3: Handle Complex URLs
Sometimes, URLs can be more complex, involving multiple levels of encoding. Let’s consider a scenario where you’re working with nested query parameters.
$baseUrl = "https://api.example.com/search"
$query = "name=Michael&company=Tech&Biz"
$encodedQuery = [System.Web.HttpUtility]::UrlEncode($query)
$url = "$baseUrl?query=$encodedQuery"
Invoke-WebRequest -Uri $urlIn this case, the entire query string is encoded, ensuring that all special characters, including ampersands, are properly escaped.
Conclusion
Escaping ampersands in URLs ensures your PowerShell scripts interact correctly with web services. By using the %26 encoding for ampersands and leveraging PowerShell’s and .NET’s encoding functions, you can avoid common pitfalls and ensure your URLs are correctly formatted. I hope you now understand how to escape ampersands in URLs with PowerShell.
You may also like:
- How to Add Double Quotes in a String in PowerShell?
- How to Escape Single Quotes in PowerShell?
- PowerShell Single vs Double Quotes
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.