103

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eetawil
  • 1,601
  • 3
  • 15
  • 26
  • 1
    See also *[Invoke-Expression considered harmful](https://blogs.msdn.microsoft.com/powershell/2011/06/03/invoke-expression-considered-harmful/)*. – Peter Mortensen Sep 08 '18 at 14:42
  • @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 argument – Andrew Savinykh Jan 29 '19 at 03:24
  • @Andrew Savinykh: One way is to launch it through cmd instead: `$someCommandStringWithDoubleQuotes | cmd`. This actually works from PowerShell scripts (e.g. invoking [CMake](http://en.wikipedia.org/wiki/CMake) (that does require the double quotes (otherwise it will silently fail))). An alternative to CMD is bash.exe ([MinGW](http://en.wikipedia.org/wiki/MinGW)) - but it may be very, very slow to start in some circumstances. – Peter Mortensen Feb 08 '19 at 12:39

4 Answers4

106

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Owen B
  • 1,291
  • 1
  • 7
  • 5
  • 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. – eetawil Aug 27 '13 at 12:31
  • 1
    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`? – Peter Mortensen Sep 08 '18 at 14:37
  • It removes them in my case rather than escaping them... I tried when writing a git commit message in PowerShell. – aderchox Apr 19 '22 at 04:33
62

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

Trung0246
  • 689
  • 1
  • 10
  • 21
BartekB
  • 8,492
  • 32
  • 33
  • 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. – eetawil Aug 27 '13 at 12:33
  • 3
    This seems unnecessarily complicated compared to using backtick (`) to escape double quotes. – saille Dec 22 '14 at 22:18
  • 9
    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! – Ian Newland Jul 21 '15 at 12:48
  • 1
    Works great! If you need to expand variables in your string, you can use `@"` and `"@` instead. – browly Jan 23 '20 at 18:47
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)`"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pranspach
  • 415
  • 4
  • 8
  • 2
    interesting: ```"['`"][^'`"]['`"]"``` works… but ```'[`'"][^`'"][`'"]'``` does not! was driving me crazy – David J Nov 03 '16 at 15:26
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}"
Gschmaaz
  • 61
  • 5