51

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?

76

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.

  • 2
    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 '17 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 '17 at 6:38
  • 1
    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 '17 at 16:12
13

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).

  • 2
    This will probably drive bash users insane. (The cmd.exe escape character is ^) – grawity Jul 30 '09 at 9:39
  • 2
    Well, if you squint then it kind of looks like a backslash. – koenigdmj Jul 31 '09 at 4:38
10

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.

1

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.\"'

  • 1
    Not in PowerShell, it doesn't. PS C:\WINDOWS\system32> Write-Host 'As they say, \"live and learn.\"' As they say, \"live and learn.\" – HopelessN00b Apr 3 '18 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 – jparnell8839 Nov 20 '18 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.