87

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?

David Alpert
  • 1,585
  • 2
  • 14
  • 11

4 Answers4

113

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.

Sled
  • 877
  • 1
  • 7
  • 11
Helvick
  • 19,579
  • 4
  • 37
  • 55
  • 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 02 '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 05 '17 at 06: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 '17 at 16:12
23

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

Adam Brand
  • 6,057
  • 2
  • 28
  • 40
12

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.

Poshoholic
  • 399
  • 1
  • 4
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.\"'

dvdccd
  • 31
  • 1
  • 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.\" – HopelessN00b Apr 03 '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). https://imgur.com/1btjoQW – jparnell8839 Nov 20 '18 at 18:14