2

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.

hellojoshhhy
  • 855
  • 2
  • 15
  • 32
  • 4
    [Quoting](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_quoting_rules) and [parsing](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_parsing) PowerShell command will probably always be a hassle. Anyways, try to workaround embedded double quotes: `powershell -Command "(gc C:\my_configuration.conf) -replace 'INSERT_URL', 'https://mytestserver/WW48.2''22/testing.bin' | Out-File C:\my_configuration.conf"` or simply [encoded the command](https://stackoverflow.com/a/73541687/1701026). – iRon Dec 20 '22 at 12:28
  • @iRon I agree with you, unfortunately I have no control over URL as it's being parsed as an argument, and it always contains `'` – hellojoshhhy Dec 21 '22 at 00:39

2 Answers2

2

Remove the ` before ", and your command should work; that is, when calling powershell.exe from cmd.exe / outside PowerShell, use \" , not \`" (or `") in order to escape " chars.:

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"..."`.

mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

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%2722/testing.bin" | Out-File 'C:\my_configuration.conf' }"

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

tonypags
  • 137
  • 6
  • There's no reason to use `"& { ... }"` in order to invoke code passed to PowerShell's CLI via the `-Command` (`-c`) parameter - just use `"..."` directly. Older versions of the [CLI documentation](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh) erroneously suggested that `& { ... }` is required, but this has since been corrected. – mklement0 Dec 20 '22 at 16:49
  • Apart from that, you still need to escape `"` chars. (which is the problem at hand), and, potentially, `%` chars. meant to be used verbatim when calling from a batch file (`cmd.exe`). Specifically, `%27` would need to be `%%27` when called from a batch file; at the interactive prompt, however, you must _not_ use `%%`. Anyway, such URL encoding is often not necessary, as it is performed on demand by various APIs. – mklement0 Dec 20 '22 at 16:58