91

When including a literal quote character inside a quoted string in Powershell, how do I escape the quote character to indicate it is a literal instead of a string delimeter?

1
  • this works: curl.exe -X POST -H "content-type:application/json" "http://localhost:8080/auth" --data '{\"username\":\"john\",\"password\":\"pwd123\"}' Jun 6, 2023 at 1:09

4 Answers 4

115

From help about_quoting_rules

To make double-quotation marks appear in a string, enclose the entire string in single quotation marks. For example:

'As they say, "live and learn."'

The output from this command is:

As they say, "live and learn."

You can also enclose a single-quoted string in a double-quoted string. For example:

"As they say, 'live and learn.'"

The output from this command is:

As they say, 'live and learn.'

To force Windows PowerShell to interpret a double quotation mark literally, use a backtick character. This prevents Windows PowerShell from interpreting the quotation mark as a string delimiter. For example:

"Use a quotation mark (`") to begin a string."

The output from this command is:

Use a quotation mark (") to begin a string.

Because the contents of single-quoted strings are interpreted literally, you cannot use the backtick character to force a literal character interpretation in a single-quoted string.

The use of the backtick character to escape other quotation marks in single quoted strings is not supported in recent versions of PowerShell. In earlier versions of PowerShell the backtick escape character could be used to escape a double quotation mark character within a single quoted string as detailed in the help about_quoting document that is available in those versions of PowerShell.

3
  • 3
    Just a note, but it appears as though you can't escape single-quotes Your last example isn't valid. Maybe something has changed since your post, as I can't find that about_quoting help either. Running powershell v5.1. Escaping double-quotes, or nesting different types of quotes still works as expected.
    – Kal Zekdor
    Jun 2, 2017 at 18:04
  • 1
    Interesting - it would appear that support for escaping single-quotes was a bug. The about_quoting help file has been replaced by about_quoting_rules and it says clearly that you cannot use a backtick to escape single quotes in PowerShell v5 because single quoted strings are literals. Good catch, this was different in V3.
    – Helvick
    Jun 5, 2017 at 6:38
  • 2
    Note that single quotes are escaped by doubling them in single-quoted strings, as in SQL. Thus 'As they say, ''live and learn.''' outputs As they say, 'live and learn.', the same as your second example.
    – brianary
    Dec 21, 2017 at 16:12
26

The escape character in Powershell is the "`" (backward apostrophe/grave).

This can be used to escape quotes and also special characters (e.g., tab is `t).

3
  • 6
    This will probably drive bash users insane. (The cmd.exe escape character is ^) Jul 30, 2009 at 9:39
  • 5
    Well, if you squint then it kind of looks like a backslash.
    – koenigdmj
    Jul 31, 2009 at 4:38
  • 1
    @user1686 you shouldn't use backticks in bash anyway. It's been deprecated
    – phuclv
    Mar 17, 2021 at 9:44
13

To compliment what has already been provided here, you should also know that you can escape a quote (single or double) with the quote itself. That means you can do this:

"Here's an example of a ""double-quoted string""."

and this:

'This time it''s ''single-quoted''.'

The advantage this syntax provides is simple: it's easier to type the same quote twice than it is to escape a quote with a backtick.

2

single 'text' so it is treated as literal text, then escape any special characters using "\"

e.g. This string: "As they say, "live and learn."" Becomes this string 'As they say, \"live and learn.\"'

2
  • 3
    Not in PowerShell, it doesn't. PS C:\WINDOWS\system32> Write-Host 'As they say, \"live and learn.\"' As they say, \"live and learn.\" Apr 3, 2018 at 18:38
  • 1
    @HopelessN00b maybe let's not judge too harshly - this works if you're using PowerShell.exe -Command and trying to pass double quotes in the parameter (as, technically, it's CMD escaping it, but is valid in the command parameter). imgur.com/1btjoQW Nov 20, 2018 at 18:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .