15

After viewing this post I am trying to escape quotes, but doing a backslash does not escape html quotes like this: <-- notice it's a right double quote unlike the normal ".

E.g:

Works fine:

powershell -noexit -command $str = \"hello '123' world\"; write-host $str

Does not work:

powershell -noexit -command $str = \"hello '123'” world\"; write-host $str

How can I escape this? Or better how do I escape ALL characters at once to get rid of this hassle!

1
  • Your "works fine" line doesn't work for me in a command prompt. I get "the string is missing the terminator: "." error. If I execute it from the run dialog box though, it's handles it ok. Commented Nov 8, 2013 at 15:28

6 Answers 6

15

Use a backtick ` to escape your special double quote, so this:

powershell -noexit -command $str = \"hello '123'” world\"; write-host $str

becomes this:

powershell -noexit -command $str = \"hello '123'`” world\"; write-host $str

EDIT:

Option 1. If you prefer using here-strings, you can change the above to powershell -file and run your powershell script file. Use here-strings as much as you want there.

Option 2. If you prefer not to deal with your special quote character inside calling/processing application/script, you can switch to using single quotes instead. In this case you only need to escape your single quotes, by doubling them, like this:

powershell -noexit -command $str = 'hello ''123''” world'; write-host $str

Notice here I did not do anything to your double quote character, and it works just fine.

So your string replace code may become slightly easier to read and maintain, especially if the editor does not display this character correctly (like Powershell console does - it shows a regular double quote instead).

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, but wouldn't this require that I know where the right-double quote is? The problem is that my $str is a variable text. It's used for Service Manager (SCSM) that are only able to launch powershell through a command prompt :(. The $str is a description field that could potentially contain special characters like the above, so if I could escape ALL it would be great.
@user2969412: You should know your special characters, so that you can escape them. And yes, you are responsible for escaping them yourself, there is no standard function in Powershell. It's a trial and error process, until you've discovered all of those special characters. You may find this answer useful.
I do know my special characters - it's only this right-double quote that is a problem. But I don't know where in the text it will happen.. In powershell you can escape everything by this @' <some block of complicated text> '@ so everything from @' to '@ will be escaped. Only I can't get this to work through a command prompt. So I figured that cmd much have something similar to this. <br><br> The here-string also needs to be on a seperate line for it to work: $str = @' hello world *'"'123 '@
@user2969412: Who/what runs this command? Is it a C# application? How is $str text passed into it? You should be able to do a string replace of to `” before you execute the command.
We're getting warmer :)
|
11
  1. Surround the -command argument with " "
  2. Strings are recognized by surrounding a text with 4 quotes on each side, """" text """".

Example:

powershell.exe -command "$s=""""A string.""""; $s"
    Output Stream:> A string.
  1. For quotes in sub expressions, double the quotes (8 on each side):
powershell.exe -command "$s=""""Use $(2*4) """"""""quote chars"""""""" in strings.""""; $s"
    Output Stream:> Use 8 "quote chars" in strings.

For clarity, here's the equivalent Powershell code:

$s="Use $(2*4) ""quote chars"" in strings."; $s

1 Comment

wow! 8 doublequotes ie. 16 single ones. Command prompt and Powershell is a perfect match ;)
3

I've faced the same issue and found out that with Powershell 6.x (Core) it's possible to do this:

pwsh -command write-host "`u{22}quoted`u{22}" 

Output:

"quoted"

Comments

1

Running this from a command prompt (and within PowerShell actually):

powershell -noexit -command { $str = "hello '123' world"; write-host $str }

generates:

hello '123' world

Does this do what you need?

3 Comments

This doesn't address the smart quote character in the OPs question. Yeah, it's hard to distinguish that character from a normal double quote on some screens. It was real clear on my Surface 2 but looking at this on my desktop, it's hard to see in the proportional font used in the question.
CTRL+++ ... ahhh I see it very clearly now. I believe the answer now lies below by Neolisk :)
Works like charm!
0

Command

powershell -command " """"Hi there `\""Bobby`\"", my friend.""""                               "
powershell -command " """"Well hello `""""my friendo`"""", why so curious?""""                 "
powershell -command " """"Hmm, why are you wondering about \""\""my curiosity?\""\"".""""      "
powershell -command " """"I sense """"""""tension"""""""", no?""""                             "
powershell -command " 'And what would the cause of this """"tension"""" be?'                   "
powershell -command " 'Oh you know, \"things\" of \"sorts\"...'                                "
powershell -command "$q=""`""""""; """"Interesting ${q}things${q} of ${q}sorts${q}?""""        "
powershell -command " """"Yes. $([char]34)THINGS$([char]34) of $([char]34)SORTS$([char]34)"""" "
powershell -command " """"This conversation is $('""""')soooo$('""""') over.""""               "
powershell -command " """"Sure $('\"')is$('\"').""""                                           "

Powershell

"Hi there `"Bobby`", my friend."
"Well hello `"my friendo`", why so curious?"
"Hmm, why are you wondering about ""my curiosity?""."
"I sense ""tension"", no?"
'And what would the cause of this "tension" be?'
'Oh you know, "things" of "sorts"...'
$q="`""; "Interesting ${q}things${q} of ${q}sorts${q}?"
"Yes. $([char]34)THINGS$([char]34) of $([char]34)SORTS$([char]34)"
"This conversation is $('"')soooo$('"') over."
"Sure $('"')is$('"')."

Output

Hi there "Bobby", my friend.
Well hello "my friendo", why so curious?
Hmm, why are you wondering about "my curiosity?".
I sense "tension", no?
And what would the cause of this "tension" be?
Oh you know, "things" of "sorts"...
Interesting "things" of "sorts"?
Yes. "THINGS" of "SORTS"
This conversation is "soooo" over.
Sure "is".

Table (various methods)

| CMD in ("""")           | PS1 in (")             | CMD in (') | PS1 in (') |
|-------------------------|------------------------|------------|------------|
|       `""""hi`""""      |         `"hi`"         |            |            |
|    """"""""hi""""""""   |         ""hi""         | ""hi""     | "hi"       |
|     $('""')hi$('""')    |     $('"')hi$('"')     |            |            |
|     $('\"')hi$('\"')    |     $('"')hi$('"')     |            |            |
|        `\""hi`\""       |         ""hi""         | \"hi\"     | "hi"       |
| $([char]34)hi$([char]34)|$([char]34)hi$([char]34)|            |            |

Comments

0

When executing powershell within a command prompt, to get the powershell interpreter to parse a literal double quote, it needs to be fed in as \". After that all the other quoting rules for powershell apply.

For example,

powershell -Command echo \"foo `\" bar\"

prints

foo bar

the same as if you were to have an interactive powershell, and typed in

echo "foo `" bar"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.