13

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!

phuclv
  • 37,963
  • 15
  • 156
  • 475
Morten_564834
  • 1,479
  • 3
  • 15
  • 26
  • 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. – Robin Nov 08 '13 at 15:28

4 Answers4

14

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

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • 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. – Morten_564834 Nov 14 '13 at 12:48
  • @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](http://stackoverflow.com/a/15246105/897326) useful. – Victor Zakharov Nov 14 '13 at 13:33
  • 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 @' '@ 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.

    The here-string also needs to be on a seperate line for it to work: $str = @' hello world *'"'123 '@
    – Morten_564834 Nov 15 '13 at 15:35
  • @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. – Victor Zakharov Nov 15 '13 at 19:34
  • We're getting warmer :) – Morten_564834 Nov 19 '13 at 13:25
  • Option 1 is a Little problematic, because the script needs to launch Outlook from their local machine and that could be problematic if they launch the script from a remote location. Option 2: The thing is that the problem starts the moment I pass the text into the script, so doing a replace needs to happen before the $str is assigned the text (which doesn't make sense). The $str is passed in via a variable from the program: $str = $Context\Property.Description, but powershell can only be launched via a command prompt, and it's in the parsing here it fails – Morten_564834 Nov 19 '13 at 13:35
  • Here is an example from the application: http://scug.be/valerie/2013/02/09/scsmcustom-console-task-change-status-to-in-progress/ She uses the taskhandler to launch a powershell script. The script is executed via a command prompt (pretty dumb yes, but that's how it's done) Thanks for your help btw – Morten_564834 Nov 19 '13 at 13:39
  • @user2969412: I am getting this using your link: `500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.` – Victor Zakharov Nov 19 '13 at 20:02
  • Works fine here. Maybe it Washington temp down. Here is another ex.: http://blogs.technet.com/b/antoni/archive/2013/03/22/setting-status-using-powershell-and-converting-this-into-a-custom-task-in-service-manager-2012-sp1.aspx Just scroll Down to the screenshots – Morten_564834 Nov 24 '13 at 19:46
5
  1. Start -command switch with ""
  2. Strings are recognized by surrounding a text with 4 times quotes at both sides: """" text """"

    Example:

    powershell.exe -command """""Recognized as string """""
    
    Output Stream:> Recognized as string
    
  3. For quotes in sub expressions double the quotes, that means 8 times quotes

    Example:

    powershell.exe -command """""""""How to quote in subexpression"""""""""
    
    Output Stream:> "How to quote in subexpression"
    
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Vinkelman
  • 117
  • 1
  • 7
  • 7
    wow! 8 doublequotes ie. 16 single ones. Command prompt and Powershell is a perfect match ;) – maoizm Jun 01 '18 at 08:22
2

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"
noseratio
  • 59,932
  • 34
  • 208
  • 486
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?

Robin
  • 1,602
  • 3
  • 16
  • 24
  • 2
    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. – Keith Hill Nov 08 '13 at 17:05
  • CTRL+++ ... ahhh I see it very clearly now. I believe the answer now lies below by Neolisk :) – Robin Nov 08 '13 at 17:12
  • 1
    Works like charm! – pascalre May 28 '21 at 13:22