Command line escaping single quote for PowerShell Command line escaping single quote for PowerShell powershell powershell

Command line escaping single quote for PowerShell


This is actually a lot trickier than you'd think. Escaping nested quotes in strings passed from cmd to PowerShell is a major headache. What makes this one especially tricky is that you need to make the replacement in a variable expanded by cmd in the quoted argument passed to powershell.exe within a single-quoted argument passed to a PowerShell script parameter. AFAIK cmd doesn't have any native functionality for even basic string replacements, so you need PowerShell to do the replacement for you.

If the argument to the -data paramater (the one contained in the cmd variable x) doesn't necessarily need to be single-quoted, the simplest thing to do is to double-quote it, so that single quotes within the value of x don't need to be escaped at all. I say "simplest", but even that is a little tricky. As Vasili Syrakis indicated, ^ is normally the escape character in cmd, but to escape double quotes within a (double-)quoted string, you need to use a \. So, you can write your batch command like this:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass "G:\test.ps1 -name \"%x%\" -data '%y%'"

That passes the following command to PowerShell:

G:\test.ps1 -name "value of x, which may contain 's" -data 'value of y'

If, however, x can also contain characters that are special characters in PowerShell interpolated strings (", $, or `), then it becomes a LOT more complicated. The problem is that %x is a cmd variable that gets expanded by cmd before PowerShell has a chance to touch it. If you single-quote it in the command you're passing to powershell.exe and it contains a single quote, then you're giving the PowerShell session a string that gets terminated early, so PowerShell doesn't have the opportunity to perform any operations on it. The following obviously doesn't work, because the -replace operator needs to be supplied a valid string before you can replace anything:

'foo'bar' -replace "'", "''"

On the other hand, if you double-quote it, then PowerShell interpolates the string before it performs any replacements on it, so if it contains any special characters, they're interpreted before they can be escaped by a replacement. I searched high and low for other ways to quote literal strings inline (something equivalent to perl's q//, in which nothing needs to be escaped but the delimiter of your choice), but there doesn't seem to be anything.

So, the only solution left is to use a here string, which requires a multi-line argument. That's tricky in a batch file, but it can be done:

setlocal EnableDelayedExpansionset LF=^set pscommand=G:\test.ps1 -name @'!LF!!x!!LF!'@ -data '!y!'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass "!pscommand!"
  • This assumes that x and y were set earlier in the batch file. If your app can only send a single-line command to cmd, then you'll need to put the above into a batch file, adding the following two lines to the beginning:

    set x=%~1set y=%~2

    Then invoke the batch file like this:

    path\test.bat "%x%" "%y%"

    The ~ strips out the quotes surrounding the command line arguments. You need the quotes in order to include spaces in the variables, but the quotes are also added to the variable value. Batch is stupid that way.

  • The two blank lines following set LF=^ are required.

That takes care of single quotes which also interpreting all other characters in the value of x literally, with one exception: double quotes. Unfortunately, if double quotes may be part of the value as you indicated in a comment, I don't believe that problem is surmountable without the use of a third party utility. The reason is that, as mentioned above, batch doesn't have a native way of performing string replacements, and the value of x is expanded by cmd before PowerShell ever sees it.

BTW...GOOD QUESTION!!


UPDATE:

In fact, it turns out that it is possible to perform static string replacements in cmd. Duncan added an answer that shows how to do that. It's a little confusing, so I'll elaborate on what's going on in Duncan's solution.

The idea is that %var:hot=cold% expands to the value of the variable var, with all occurrences of hot replaced with cold:

D:\Scratch\soscratch>set var=You're such a hot shot!D:\Scratch\soscratch>echo %var%You're such a hot shot!D:\Scratch\soscratch>echo %var:hot=cold%You're such a cold scold!

So, in the command (modified from Duncan's answer to align with the OP's example, for the sake of clarity):

powershell G:\test.ps1 -name '%x:'=''%' -data '%y:'=''%'

all occurrences of ' in the variables x and y are replaced with '', and the command expands to

powershell G:\test.ps1 -name 'a''b' -data 'c''d'

Let's break down the key element of that, '%x:'=''%':

  • The two 's at the beginning and the end are the explicit outer quotes being passed to PowerShell to quote the argument, i.e. the same single quotes that the OP had around %x
  • :'='' is the string replacement, indicating that ' should be replaced with ''
  • %x:'=''% expands to the value of the variable x with ' replaced by '', which is a''b
  • Therefore, the whole thing expands to 'a''b'

This solution escapes the single quotes in the variable value much more simply than my workaround above. However, the OP indicated in an update that the variable may also contain double quotes, and so far this solution still doesn't pass double quotes within x to PowerShell--those still get stripped out by cmd before PowerShell receives the command.

The good news is that with the cmd string replacement method, this becomes surmountable. Execute the following cmd commands after the initial value of x has already been set:

  1. Replace ' with '', to escape the single quotes for PowerShell:

    set x=%x:'=''%
  2. Replace " with \", to escape the double quotes for cmd:

    set x=%x:"=\"%

    The order of these two assignments doesn't matter.

  3. Now, the PowerShell script can be called using the syntax the OP was using in the first place (path to powershell.exe removed to fit it all on one line):

    powershell.exe -ExecutionPolicy Bypass "G:\test.ps1 -name '%x' -data '%y'"

Again, if the app can only send a one-line command to cmd, these three commands can be placed in a batch file, and the app can call the batch file and pass the variables as shown above (first bullet in my original answer).

One interesting point to note is that if the replacement of " with \" is done inline rather than with a separate set command, you don't escape the "s in the string replacement, even though they're inside a double-quoted string, i.e. like this:

powershell.exe -ExecutionPolicy Bypass "G:\test.ps1 -name '%x:"=\"' -data '%y'"

...not like this:

powershell.exe -ExecutionPolicy Bypass "G:\test.ps1 -name '%x:\"=\\"' -data '%y'"


I'm slightly unclear in the question whether %x and %y are CMD variables (in which case you should be using %x% to substitute it in, or a substitution happening in your other application.

You need to escape the single quote you are passing to PowerShell by doubling it in the CMD.EXE command line. You can do this by replacing any quotes in the variable with two single quotes.

For example:

C:\scripts>set X=a'bC:\scripts>set Y=c'dC:\scripts>powershell .\test.ps1 -name '%x:'=''%' '%y:'=''%'Name is 'a'b'Data is 'c'd'

where test.ps1 contains:

C:\scripts>type test.ps1param($name,$data)write-output "Name is '$name'"write-output "Data is '$data'"

If the command line you gave is being generated in an external application, you should still be able to do this by assigning the string to a variable first and using & to separate the commands (be careful to avoid trailing spaces on the set command).

set X=a'b& powershell .\test.ps1 -name '%x:'=''%'

The CMD shell supports both a simple form of substitution, and a way to extract substrings when substituting variables. These only work when substituting in a variable, so if you want to do multiple substitutions at the same time, or substitute and substring extraction then you need to do one at a time setting variables with each step.

Environment variable substitution has been enhanced as follows:    %PATH:str1=str2%would expand the PATH environment variable, substituting each occurrenceof "str1" in the expanded result with "str2".  "str2" can be the emptystring to effectively delete all occurrences of "str1" from the expandedoutput.  "str1" can begin with an asterisk, in which case it will matcheverything from the beginning of the expanded output to the firstoccurrence of the remaining portion of str1.May also specify substrings for an expansion.    %PATH:~10,5%would expand the PATH environment variable, and then use only the 5characters that begin at the 11th (offset 10) character of the expandedresult.  If the length is not specified, then it defaults to theremainder of the variable value.  If either number (offset or length) isnegative, then the number used is the length of the environment variablevalue added to the offset or length specified.    %PATH:~-10%would extract the last 10 characters of the PATH variable.    %PATH:~0,-2%would extract all but the last 2 characters of the PATH variable.


I beleive you can escape it with ^:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass "G:\test.ps1 -name ^'%x^' -data ^'%y^'"