TransWikia.com

pwsh -Command is removing quotation marks

Stack Overflow Asked by Chris G. on November 20, 2021

In pwsh call the following:

Write-Host '{"drop_attr": "name"}' 

Result ok:

{"drop_attr": "name"}

Now do the same via pwsh:

pwsh -Command Write-Host '{"drop_attr": "name"}'

Result is missing quotation marks and square brackets?

drop_attr: name

3 Answers

Another way is to use "encoded commands".

> $cmd1 = "Write-Host '{ ""description"": ""Test program"" }'"
> pwsh -encoded ([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($cmd1)))
{ "description": "Test program" }

Answered by robbie fan on November 20, 2021

Another way. Are you in Windows or Unix?

pwsh -c "[pscustomobject]@{drop_attr='name'} | convertto-json -compress"

{"drop_attr":"name"}

Answered by js2010 on November 20, 2021

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.

Answered by mklement0 on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP