0

Cold you help me with following problem?

How to run the

"{0:n10}" -f 21.21

code in

powershell.exe -command "& {}" 

argument?

1 Answers1

3
  • 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 erroneously suggested that & { ... } is required, but this has since been corrected.
  • Unescaped " characters on the command line are stripped before the resulting argument(s) following -Command (-c) are interpreted as PowerShell code. " characters that need to be preserved as part of the PowerShell command to execute therefore need escaping.

    • From PowerShell's perspective, using \" works, in both editions.
    • Additionally, to prevent inadvertent whitespace normalization (folding of multiple spaces into one), the entire command to pass to -Command (-c) should be passed inside a single, "..." string overall:

Therefore (note that this assumes calling from outside PowerShell):

powershell.exe -Command " \"{0:n10}\" -f 21.21"

However, when calling from cmd.exe, specifically, \" escaping may break cmd.exe's syntax, in which case a different form of escaping of embedded " chars. is required - the following works robustly:

See this answer for details.

mklement0
  • 382,024
  • 64
  • 607
  • 775