How to Escape Dollar Signs in PowerShell?

If you are working with dollar signs in PowerShell, then you might understand the problem of dealing with special characters like the dollar sign ($). In this tutorial, I will explain different methods to escape dollar signs in PowerShell with examples.

To escape a dollar sign in PowerShell, you can use the backtick character (). The backtick serves as an escape character, allowing you to include a literal dollar sign in your string. For example, you can write Write-Host “The total cost is $100.” to correctly display “The total cost is $100.” without PowerShell interpreting $100 as a variable. This method ensures that the dollar sign is treated as a regular character within the string.

Escape Dollar Signs in PowerShell

In PowerShell, the dollar sign ($) is used to denote variables. For instance, $name is a variable that can hold a value. However, when you need to include a literal dollar sign in a string, you must escape it to prevent PowerShell from treating it as a variable.

Method 1: Using the Backtick (`)

The backtick ( `) is the escape character in PowerShell. You can use it to escape the dollar sign and other special characters.

Syntax

`$

Example

Let me show you an example.

Let’s say you want to print a string that includes a dollar sign, like a price.

Write-Host "The total cost is `$100."

This will output:

The total cost is $100.

I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

Escape Dollar Signs in PowerShell

Check out PowerShell Variables in Quotes

Method 2: Using Single Quotes

Another way to handle dollar signs is by using single quotes ('). Single quotes create a literal string, meaning that variables and special characters inside the quotes are not expanded.

Syntax

'string with $'

Example

Here is an example.

Write-Host 'The total cost is $100.'

This will also output:

The total cost is $100.

Here is the output in the screenshot below:

powershell escape dollar sign

Check out Escape Ampersands in URLs with PowerShell

Method 3: Double Quotes with Backtick

If you need to include both variables and literal dollar signs in a string in PowerShell, you can combine double quotes with the backtick.

Syntax

"string with `$ and $variable"

Example

Here is an example.

$price = 100
Write-Host "The total cost is `$${price}."

This will output:

The total cost is $100.

Here is the exact output you can see in the screenshot below:

How to Escape Dollar Signs in PowerShell

Read Add Double Quotes in a String in PowerShell

Examples: Escape Dollar Signs in PowerShell

Now, let me show you a few examples of how to escape dollar signs in PowerShell.

Example 1: File Paths

When dealing with file paths that include dollar signs, you can use the backtick to escape them. Here is the script you can use.

$path = "C:\Users\JohnDoe\Documents\`$Reports"
Write-Host $path

Example 2: Command Line Arguments

If you need to pass a dollar sign as part of a command line argument, you can escape it using the backtick. Here is the complete script.

Start-Process "cmd.exe" -ArgumentList "/c echo `$100"

Example 3: JSON Strings

You can combine single quotes and backticks in PowerShell when working with JSON strings that include dollar signs. Here is an example.

$json = '{ "price": "`$100" }'
Write-Host $json

Conclusion

In this tutorial, I explained how to escape dollar signs in PowerShell using different methods, such as the backtick, single quotes, or a combination of both. I also showed three different examples.

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.