Home > front end >  Escape double quotes in powershell -Command
Escape double quotes in powershell -Command

Time:12-21

Due to some limitations, I have to execute the Power Shell command from Windows Command Prompt

powershell -Command "(gc C:\my_configuration.conf) -replace 'INSERT_URL', \`"https://mytestserver/WW48.2'22/testing.bin\`" | Out-File C:\my_configuration.conf"

However, I am constantly getting the ParserError like below

The string is missing the terminator: '.
      CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

How should I properly wrap the URL string with double quotes? Thanks for answering.

CodePudding user response:

Try using this syntax, always works

"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "& { <# PUT ANYTHING HERE #> }"

You won't need to worry about escaping anything.

Your code:

"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "& { (gc C:\my_configuration.conf) -replace 'INSERT_URL', "https://mytestserver/WW48.2'22/testing.bin" | Out-File 'C:\my_configuration.conf' }"

EDIT1: Check here for URL special characters. the single quote (') can be handled by its replacement (') in your hard-coded string. (I changed it above in the 2nd code sample)

CodePudding user response:

Remove the ` before ", and your command should work; that is, when calling powershell.exe from cmd.exe, use \" , not \`" (or `"):

powershell -Command "(gc C:\my_configuration.conf) -replace 'INSERT_URL', \"https://mytestserver/WW48.2'22/testing.bin\" | Out-File C:\my_configuration.conf"

While you do need to escape the " characters embedded in your overall "..." command string, escaping them as \" is sufficient - no need to also use `, the backtick, PowerShell's usual escape character.

The PowerShell CLI (powershell.exe) expects \-escaping of ", so as to better align with most CLIs, even though inside a PowerShell session you need to use `" or (inside "..." only) "".[1]

You'd only need both \ and ` - in the form `\", note that ` comes first - if your embedded "..." itself contained " chars; a contrived example:

:: OK: Prints '3" of snow.'
powershell.exe -c " Write-Output \"3`\" of snow.\" "

As iRon notes, an alternative solution is to use embedded '...' quoting (single-quoting) instead.

Since your URL itself contains a ' char., that character must then be escaped as '':

:: Note the use of '...' around https://... and the inner ' escaped as ''
powershell -Command "(gc C:\my_configuration.conf) -replace 'INSERT_URL', 'https://mytestserver/WW48.2''22/testing.bin' | Out-File C:\my_configuration.conf"

[1] In PowerShell (Core) 7 , whose CLI is pwsh.exe, you may alternatively use "" inside overall "..." on the command line too, which is actually the more robust choice when calling from cmd.exe. When calling powershell.exefromcmd.exe, the robust choice is "^""(sic) - see [this answer](https://stackoverflow.com/a/49060341/45375). However, the PowerShell CLI recognizes"in _both_ editions, and"also works for"chars. _not_ inside overall"..."`.

  • Related