Cold you help me with following problem?
How to run the
"{0:n10}" -f 21.21
code in
powershell.exe -command "& {}"
argument?
Cold you help me with following problem?
How to run the
"{0:n10}" -f 21.21
code in
powershell.exe -command "& {}"
argument?
There's no reason to use "& { ... }"
in order to invoke code passed to PowerShell's CLI via the -Command
(-c
) parameter - just use "..."
directly.
& { ... }
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.
\"
works, in both editions.-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:
"^""
(sic) with powershell.exe
, the Windows PowerShell CLI:
powershell.exe -Command " "^""{0:n10}"^"" -f 21.21"
""
with pwsh.exe
, the PowerShell (Core) 7+ CLI:
pwsh.exe -Command " ""{0:n10}"" -f 21.21"
See this answer for details.