Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 17 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.

share|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

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

share|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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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