Quoting -replace & variables Quoting -replace & variables powershell powershell

Quoting -replace & variables


In your second line:

'abc' -replace 'a(\w)', "$1"

Powershell replaces the $1 before it gets to the regex replace operation, as others have stated. You can avoid that replacement by using a backtick, as in:

'abc' -replace 'a(\w)', "`$1"

Thus, if you had a string in a variable $prefix which you wanted to include in the replacement string, you could use it in the double quotes like this:

'abc' -replace 'a(\w)', "$prefix`$1"


When you use single quotes you tell PowerShell to use a string literal meaning everything between the opening and closing quote is to be interpreted literally.

When you use double quotes, PowerShell will interpret specific characters inside the double quotes.

See get-help about_quoting_rules or click here.

The dollar sign has a special meaning in regular expressions and in PowerShell. You want to use the single quotes if you intend the dollar sign to be used as the regular expression.

In your example the regex a(\w) is matching the letter 'a' and then a word character captured in back reference #1. So when you replace with $1 you are replacing the matched text ab with back reference match b. So you get bc.

In your second example with using double quotes PowerShell interprets "$1" as a string with the variable $1 inside. You don't have a variable named $1 so it's null. So the regex replaced ab with null which is why you only get c.


The '$1' is a regex backreference. It's created by the regex match, and it only exists within the context of that replace operation. It is not a powershell variable.

"$1" will be interpreted as a Powershell variable. If no variable called $1 exists, the replacement value will be null.