Unfortunately, PowerShell's handling of passing arguments with embedded "
chars. to external programs - which includes PowerShell's own CLI (pwsh
) - is fundamentally broken (and always has been), up to at least PowerShell 7.1:
You need to manually
-escape "
instances embedded in your arguments in order for them to be correctly passed through to external programs:
pwsh -Command ' ''{"drop_attr": "name"}'' '
Note that I'm assuming your intent is to pass a JSON string, hence the inner '' ... ''
quoting (escaped single quotes), which ensures that pwsh
ultimately sees a single-quoted string ('...'
). (No need for an explicit output command; PowerShell implicitly prints command and expression output).
Note that from inside PowerShell, you can avoid the need for
-escaping, if you call pwsh
with a script block ({ ... }
) - but that only works when calling PowerShell itself, not other external programs:
# NOTE: Works from PowerShell only.
pwsh -Command { '{"drop_attr": "name"}' }
Background info on PowerShell's broken handling of arguments with embedded "
in external-program calls, as of PowerShell 7.1:
This GitHub docs issue contains background information.
GitHub issue #1995 discusses the problem and the details of the broken behavior as well as manual workarounds are summarized in this comment; the state of the discussion as of PowerShell [Core] 7 seems to be:
A fix is being considered as an experimental feature, for v7.2 at the earliest. Whether it will become a regular feature - i.e whether the default behavior will be fixed or whether the fix will require opt-in or even if the feature will become official at all - remains to be seen.
- Fixing the default behavior would substantially break backward compatibility; as of this writing, this has never been allowed, but a discussion as to whether to allow breaking changes in the future and how to manage them has begun: see GitHub issue #13129.
See GitHub PR #14692 for the relevant experimental feature, which, however, as of this writing is missing vital accommodations for batch files and msiexec
-style executables on Windows - see GitHub issue #15143.
In the meantime, you can use the PSv3+ ie
helper function from the Native
module (in PSv5+, install with Install-Module Native
from the PowerShell Gallery), which internally compensates for all broken behavior and allows passing arguments as expected; e.g.,
ie pwsh -Command ' ''{"drop_attr": "name"}'' '
would then work properly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…