Skip to content

CLI: pwsh -Command - and pwsh -File - unexpectedly exhibit pseudo-interactive behavior, lack support for argument passing #3223

@mklement0

Description

@mklement0

Related: #9497

While pwsh -Command - and pwsh -File - both accept command input from the pipeline / from stdin:

  • they exhibit undesirable pseudo-interactive behavior:

    • they execute each line one by one, in separate pipelines.

      • additionally, -File - prints the prompt string and each input line before the output
    • they require pipelines that are spread across multiple lines for readability to be terminated with two newlines (see below) - as would be necessary interactively, if PSReadLine weren't loaded.

  • they do not support combining - input with passing arguments:

    • While that limitation is documented for -Command - (-File - is undocumented altogether), there's no good reason for this limitation, and it limits the usefulness of the construct.

    • While you could (but in my mind shouldn't) argue that -Command - should be limited to providing a self-contained source-code snippet that doesn't expect arguments, at the least
      -File - should support passing arguments by placing them after the - - after all, you want to be able to pass arguments to a script - whether that script is specified as a a file or as a script's content via stdin.

Thus, the following command, which spreads pipeline "hi" | % { "$_ there" } across two lines - does not work:

@'
"in"
"hi" |
  % { "$_ there" }
"out"
'@ | powershell -noprofile -command -

The above yields only in, because the end of the multi-line command is never detected, causing it to be quietly ignored.

As evidence that this happens in the real world, see this Stack Overflow question, whose accepted answer provides more detail.

Inserting an empty line after the multi-line command fixes the problem:

@'
"in"
"hi" |
  % { "$_ there" }

"out"
'@ | powershell -noprofile -command -

The above now yields in, hi there and out - that is, the multi-line pipeline was correctly submitted due to the extra newline, as was the single-line statement afterwards.


Summary:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifWG-Interactive-Consolethe console experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions