How to Escape Dollar Signs in PowerShell (2 Methods)


There are two methods you can use to escape dollar signs in PowerShell:

Method 1: Use Single Quotes when Defining Variables

$my_file = 'c:\users\bobbi\data$new'

By using single quotes when defining a variable, you can prevent PowerShell from expanding $ into a variable.

If you instead use double quotes, then PowerShell will automatically expand $ into a variable.

Method 2: Use Backtick ( ` ) to Escape Dollar Sign

$my_file = 'c:\users\bobbi\data`$new'

By using a backtick ( ` ) you can escape a dollar sign as well.

The following examples show how to use each method in practice.

Example 1: Use Single Quotes to Escape Dollar Signs in PowerShell

Suppose that we have a folder located at the following path:

  • c:\users\bobbi\data$new

Suppose that we attempt to use the following syntax to define this variable path as a string in PowerShell:

$my_file = "c:\users\bobbi\data$new"

The following screenshot shows how to use this syntax in practice:

Notice that PowerShell automatically converts $new to a variable and removes it from the string.

To escape this dollar sign when defining this string, we could instead use single quotes:

$my_file = 'c:\users\bobbi\data$new'

The following screenshot shows how to use this syntax in practice:

PowerShell escape dollar sign with single quotes

Since we used single quotes when defining the variable, PowerShell did not expand the $ into a variable.

Example 2: Use Backtick to Escape Dollar Signs in PowerShell

Once again suppose that we have a folder located at the following path:

  • c:\users\bobbi\data$new

Another way to escape the dollar sign when defining the file path as a string is to use a backtick in front of the dollar sign:

$my_file = "c:\users\bobbi\data`$new"

The following screenshot shows how to use this syntax in practice:

PowerShell escape dollar sign with backtick

By using a backtick right before the dollar sign, we are able to escape the dollar sign and keep it in the string.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Extract Text Between Two Strings
PowerShell: How to Extract Only Numbers from String
PowerShell: How to Replace Special Characters in String

Leave a Reply