How to Escape Special Characters in Variables in PowerShell?

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 $example

In 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:

escape special characters in variable powershell

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 $price

Here, the dollar sign is escaped to ensure it is treated as a literal character.

You can see the exact output in the screenshot below:

powershell escape special characters in variable

Escape Backticks

Since the backtick itself is the escape character, escaping it requires doubling up.

$example = "This is a backtick: ``"
Write-Output $example

Check 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 $example

Here is the output in the screenshot below:

escape special characters in variable in powershell

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 $escapedExample

In 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 $escapedCommand

Conclusion

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.