Escape Double Quotes in PowerShell (Strings, Variables, JSON and .exe Arguments)

To escape double quotes in PowerShell, put a backtick in front of each one (`") or type it twice ("") inside a double-quoted string. Or wrap the whole text in single quotes, where a double quote needs no escaping:

"She said `"yes`" to the Austin office."
"She said ""yes"" to the Austin office."
'She said "yes" to the Austin office.'

Output:

She said "yes" to the Austin office.
She said "yes" to the Austin office.
She said "yes" to the Austin office.

All three lines print the same sentence. Strings are the easy part, though. Passing quotes to an .exe works differently in PowerShell 7 and Windows PowerShell 5.1.

In this tutorial, I’ll show you how to escape double quotes in PowerShell strings and variables, here-strings, smart quotes, JSON and CSV, and in arguments for native programs. I tested every example in PowerShell 7.6 and 5.1.

How do you escape double quotes in a PowerShell string?

PowerShell gives you three ways to add double quotes to a string, and you saw them all above:

  • Backtick: `" makes the quote literal. The backtick is the PowerShell escape character for every special character.
  • Doubled quotes: "" inside a double-quoted string becomes one ".
  • Single quotes: '..."...' keeps everything as typed, but variables don’t expand.
Escape double quotes in PowerShell with a backtick, doubled quotes and single quotes
A backtick, doubled quotes and single quotes all print the same quote marks (PowerShell 7)

A backtick inside single quotes is just a backtick, so '`"' prints both characters. That’s why you pick the method based on the outer quotes.

When the text holds a variable, use double quotes with a backtick or doubled quotes. For fixed text, single quotes are the least typing. The difference between single and double quotes matters for more than quotes.

Add double quotes around a variable

This is the most common real job, such as quoting a file path with a space in it. Here are five ways to wrap $path in quotes:

$path = 'C:\Reports\Q3 Sales.xlsx'

"`"$path`""
"Open ""$path"" in Excel"
'"' + $path + '"'
'"{0}"' -f $path
"$([char]34)$path$([char]34)"

Output:

"C:\Reports\Q3 Sales.xlsx"
Open "C:\Reports\Q3 Sales.xlsx" in Excel
"C:\Reports\Q3 Sales.xlsx"
"C:\Reports\Q3 Sales.xlsx"
"C:\Reports\Q3 Sales.xlsx"
Add double quotes around a variable in Windows PowerShell 5.1
Five ways to put quotes around a path stored in a variable (Windows PowerShell 5.1)

The first two expand the variable inside the string. The others concatenate strings with the + operator, use the -f format operator, or insert [char]34, the character code of a double quote.