Home > Back-end >  Launching powershell script from cmd without using double quotes (")
Launching powershell script from cmd without using double quotes (")

Time:10-26

I need to launch a powershell command from cmd without using double quotes "

My current command:

powershell.exe -noexit Start-BitsTransfer -Source 'download link' -Destination 'C:\test.txt'

The error:

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

The reason I cannot use double quotes is because I'm using the post-install-command= parameter from vboxmanage which already uses double quotes.

CodePudding user response:

  • It's best to enclose everything starting with Start-BitsTransfer in (escaped) "...", i.e., to pass a single, double-quoted string to the (positionally implied) -Command parameter of powershell.exe), which prevents cmd.exe from interpreting the string's content (except, potentially, cmd.exe-format environment-variable references such as %OS%).

    • Without "...", as in your case, a & outside a "..." string is then interpreted as a cmd.exe metacharacter (statement separator), and the call breaks ('...' strings are only recognized by PowerShell, not by cmd.exe).

    • While you could ^-escape all cmd.exe metacharacters individually, enclosing the entire -Command argument in "..." is the simpler solution. (Also, even though it is rarely a problem in practice, whitespace normalization occurs in the absence of "..." enclosure, i.e. runs of multiple spaces are folded into one space per run).

  • Since your entire powershell.exe command is itself embedded in a "..." string passed as part of an option to vboxmanage, you must escape the " chars. that are part of the powershell.exe call, namely as \".

Therefore, in the context of your vboxmanage call, use something like the following - note the outer "..." and embedded inner \"...\":

vboxmanage ... post-install-command="powershell.exe -noexit \"Start-BitsTransfer -Source 'download link' -Destination 'C:\test.txt'\""
  • Related