Recently, in one of my projects, a team member tried to escape special characters in a PowerShell variable. I suggested a few methods. In this tutorial, I will show you how to escape special characters in variables in PowerShell with examples.
To escape special characters in PowerShell variables, use the backtick (`) as the escape character. For example, to include double quotes within a string, you can write: $example = "He said, “Hello, World!"". This tells PowerShell to treat the double quotes as literal characters, ensuring your script runs smoothly without misinterpreting the special characters.
Escape Special Characters in Variables in PowerShell
Special characters in PowerShell include symbols like <, >, &, |, ^, and more. These characters have specific meanings in the PowerShell language and can alter the way your scripts run. To ensure your scripts run smoothly, it’s crucial to escape these characters when they appear in strings or variables.
PowerShell uses the backtick (`) as its escape character. This character tells PowerShell to treat the following character as a literal rather than a special character.
Now, let me show you some examples.
The basic syntax for escaping a special character in PowerShell is:
`<special_character>Escape Double Quotes
Double quotes are often used in strings, and escaping them can be essential when they appear within the string itself.
$example = "He said, `"Hello, World!`""
Write-Output $exampleIn this example, the double quotes around Hello, World! are escaped using the backtick.
Here is the exact output you can see in the screenshot below:

Read PowerShell Variables in Quotes
Escape Dollar Signs
Dollar signs ($) are used to denote variables in PowerShell. If you need to include a dollar sign in a string without it being interpreted as a variable, you must escape it.
$price = "The total is `$100"
Write-Output $priceHere, the dollar sign is escaped to ensure it is treated as a literal character.
You can see the exact output in the screenshot below:

Escape Backticks
Since the backtick itself is the escape character, escaping it requires doubling up.
$example = "This is a backtick: ``"
Write-Output $exampleCheck out How to Check if a Variable Exists in PowerShell?
Using Single Quotes
Another method to handle special characters is by using single quotes ('). Strings enclosed in single quotes are treated as literal strings, meaning special characters are not interpreted.
$example = 'This is a string with special characters like $ and " without escaping.'
Write-Output $exampleHere is the output in the screenshot below:

The -replace Operator
You might need to replace special characters within a string for more complex scenarios. PowerShell’s -replace operator can come in handy here.
$example = "Escape < and > in this string"
$escapedExample = $example -replace "<", "`<" -replace ">", "`>"
Write-Output $escapedExampleIn this example, both < and > are replaced with their escaped versions.
Using \ for Native Commands
When working with native commands that require escaping, the backslash (\) is often used. This is particularly relevant when passing arguments to external programs.
$command = 'C:\Program Files\Example\example.exe'
$escapedCommand = $command -replace " ", "\ "
Write-Output $escapedCommandConclusion
In this tutorial, I explained how to escape special characters in PowerShell using different methods, such as using the backtick (), single quotes, and the -replace` operator, etc. I hope this helps.
You may also like:
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.