-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
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
- additionally,
-
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
PSReadLineweren'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:
-
Both
File -and-Command -should be fixed; and, on Unix/dev/stdinshould be the same as passing-. -
If
-File -worked properly, there'd really be no need for-Command -.- Update: Except perhaps for emulating an interactive REPL via stdin - see Script via STDIN does not exit with error code and does not stop correctly #15331 (comment)
-
-File -should optionally also accept arguments.