Philippe's helpful answer provides the crucial pointer:
When calling pwsh
, the PowerShell (Core) CLI, from outside PowerShell:
Do not use { ... }
to enclose your command(s) - that only works from inside PowerShell.
Inside PowerShell, { ... }
creates a script block literal, which is a reusable piece of PowerShell code that can be invoked on demand.
If you pass { ... }
to pwsh
from inside PowerShell, special processing happens behind the scenes, which notably preserves type fidelity in the output as much as possible - see this answer for details.
It should be noted that calling the PowerShell CLI from PowerShell is rarely necessary, except, for instance, on Windows, if you want to call the respective other PowerShell edition's CLI.
Instead, pass your command(s) as-is, inside a string.
If you pass a string to -Command
from outside PowerShell with { ... }
embedded, what happens is that PowerShell creates a script block and - in the absence of invoking it, with &
or .
- its string representation is output, which is the block's verbatim content (excluding {
and }
) - that is what you saw.
As an aside: There are many examples out there that use "& { ... }"
as a -Command
argument; while that technically works, it is unnecessary, and passing just "..."
(or '...'
, from POSIX-compatible shells) is sufficient.
Thus:
# Simple case: no quoting necessary
$ pwsh.exe -Command Get-Date
# Command string that requires quoting:
$ pwsh.exe -Command 'Write-Output "hello"'