Escaping quotes and double quotes
Asked Answered
G

5

105

How do I properly escape the quotes in the -param value in the following command line?

$cmd="\\server\toto.exe -batch=B -param="sort1;parmtxt='Security ID=1234'""
Invoke-Expression $cmd 

This of course fails. I tried to escape the quotes (single and double) using the escape character ` and did various combination, but nothing is working.

Glottology answered 8/8, 2013 at 0:50 Comment(3)
See also Invoke-Expression considered harmful.Perish
@PeterMortensen there problem with this article, is that there does not seem to be a better way, when you want to launch a native application and pass a parameter that includes a double quote as a an argumentDroplet
@Andrew Savinykh: One way is to launch it through cmd instead: $someCommandStringWithDoubleQuotes | cmd. This actually works from PowerShell scripts (e.g. invoking CMake (that does require the double quotes (otherwise it will silently fail))). An alternative to CMD is bash.exe (MinGW) - but it may be very, very slow to start in some circumstances.Perish
B
63

Escaping parameters like that is usually source of frustration and feels a lot like a time wasted. I see you're on v2 so I would suggest using a technique that Joel "Jaykul" Bennet blogged about a while ago.

Long story short: you just wrap your string with @' ... '@ :

Start-Process \\server\toto.exe @'
-batch=B -param="sort1;parmtxt='Security ID=1234'"
'@

(Mind that I assumed which quotes are needed, and which things you were attempting to escape.) If you want to work with the output, you may want to add the -NoNewWindow switch.

BTW: this was so important issue that since v3 you can use --% to stop the PowerShell parser from doing anything with your parameters:

\\server\toto.exe --% -batch=b -param="sort1;paramtxt='Security ID=1234'"

... should work fine there (with the same assumption).

Bannockburn answered 8/8, 2013 at 7:18 Comment(4)
Thanks for you answer. start-process no matter how I tried always complained. SO I did cmd /C $cmd (where $cmd is my entire command including the .exe.). I'm using powershell 2.0. Even the here-string didn't work if I remember correctly.Glottology
This seems unnecessarily complicated compared to using backtick (`) to escape double quotes.Broughton
This works great as long as you follow the rules it expects. The first @' must be on "line 1", your parameter must be on "line 2" and the ending '@ must on "line 3". The ending '@ can't have spaces before it. Man this syntax is picky!Mezereum
Works great! If you need to expand variables in your string, you can use @" and "@ instead.Astronomy
H
108

Using the backtick (`) works fine for me if I put them in the following places:

$cmd="\\server\toto.exe -batch=B -param=`"sort1;parmtxt='Security ID=1234'`""

$cmd returns as:

\\server\toto.exe -batch=B -param="sort1;parmtxt='Security ID=1234'"

Is that what you were looking for?

The error PowerShell gave me referred to an unexpected token 'sort1', and that's how I determined where to put the backticks.

The @' ... '@ syntax is called a "here string" and will return exactly what is entered. You can also use them to populate variables in the following fashion:

$cmd=@'
"\\server\toto.exe -batch=B -param="sort1;parmtxt='Security ID=1234'""
'@

The opening and closing symbols must be on their own line as shown above.

Homegrown answered 19/8, 2013 at 12:16 Comment(3)
Sorry for the delay...the way I made it work is to finally call cmd /C "MYCOMMAND" from powershell. It didn't work using powershell directly.Glottology
But doesn't Invoke-Expression strip the double quotes from $cmd when it is invoked? Or how did you use $cmd without using Invoke-Expression?Perish
It removes them in my case rather than escaping them... I tried when writing a git commit message in PowerShell.Junction
B
63

Escaping parameters like that is usually source of frustration and feels a lot like a time wasted. I see you're on v2 so I would suggest using a technique that Joel "Jaykul" Bennet blogged about a while ago.

Long story short: you just wrap your string with @' ... '@ :

Start-Process \\server\toto.exe @'
-batch=B -param="sort1;parmtxt='Security ID=1234'"
'@

(Mind that I assumed which quotes are needed, and which things you were attempting to escape.) If you want to work with the output, you may want to add the -NoNewWindow switch.

BTW: this was so important issue that since v3 you can use --% to stop the PowerShell parser from doing anything with your parameters:

\\server\toto.exe --% -batch=b -param="sort1;paramtxt='Security ID=1234'"

... should work fine there (with the same assumption).

Bannockburn answered 8/8, 2013 at 7:18 Comment(4)
Thanks for you answer. start-process no matter how I tried always complained. SO I did cmd /C $cmd (where $cmd is my entire command including the .exe.). I'm using powershell 2.0. Even the here-string didn't work if I remember correctly.Glottology
This seems unnecessarily complicated compared to using backtick (`) to escape double quotes.Broughton
This works great as long as you follow the rules it expects. The first @' must be on "line 1", your parameter must be on "line 2" and the ending '@ must on "line 3". The ending '@ can't have spaces before it. Man this syntax is picky!Mezereum
Works great! If you need to expand variables in your string, you can use @" and "@ instead.Astronomy
M
9

I found myself in a similar predicament today while trying to run a command through a Node.js module:

I was using the PowerShell and trying to run:

command -e 'func($a)'

But with the extra symbols, PowerShell was mangling the arguments. To fix, I back-tick escaped double-quote marks:

command -e `"func($a)`"
Ministerial answered 15/1, 2014 at 3:20 Comment(1)
interesting: "['`"][^'`"]['`"]" works… but '[`'"][^`'"][`'"]' does not! was driving me crazyDarindaring
T
3

In Powershell 5 escaping double quotes can be done by backtick (`). But sometimes you need to provide your double quotes escaped which can be done by backslash + backtick (\`). Eg in this curl call:

C:\Windows\System32\curl.exe -s -k -H "Content-Type: application/json" -XPOST localhost:9200/index_name/inded_type -d"{\`"velocity\`":3.14}"
Trattoria answered 8/12, 2020 at 9:33 Comment(0)
Z
0

It looks like the editor (Microsoft) provided bugged parser. indeed this does not work (escaped double quotes with backtick):

> &sc.exe config LixFax binpath="`"C:\Work\net48\Fax.exe`" 9002"

when this works (bactick escaped with backslash in addition of backtick escaping double quotes, LOL...):

> &sc.exe config LixFax binpath="\`"C:\Work\net48\Fax.exe\`" 9002"

Thus you i manage to set the binpath with doublequotes around the .exe part of service and add additonal parameter after but still part of the binpath parameter of a windows service. And yes in this case the legacy cmd prompt is indeed smarter with simple backslash to escape double quotes!

Zamindar answered 3/4 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.