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?

link|improve this question
feedback

3 Answers

up vote 14 down vote accepted

From help about_quoting

You can nest double quotation marks in pairs of single quotation marks. The following example demonstrates how this feature can be used:

Write-Host 'As they say, "live and learn."'

The output from this command is:

As they say, "live and learn."

You can also nest single quotation marks within pairs of double quotation marks.

By using the ` escape character, you can preserve a quotation mark of the same type within a pair, as shown:

Write-Host 'It`'s easy to write literal statements in PowerShell.'

It's easy to write literal statements in PowerShell.

link|improve this answer
I like this answer because in addition to answering the question as stated, it also provides additional information about how to find the answer from within Powershell. Thank you. – David Alpert Jul 29 '09 at 23:26
feedback

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

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

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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