Repository navigation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I see the following in current PowerShell versions back to the days of powershell.exe.
Having the following files:
$ xxd lf.ps1
00000000: 4765 742d 5072 6f63 6573 7320 7c0a 5768 Get-Process |.Wh
00000010: 6572 652d 4f62 6a65 6374 207b 245f 2e4e ere-Object {$_.N
00000020: 616d 6520 2d49 4c69 6b65 2027 2a65 7870 ame -ILike '*exp
00000030: 6c6f 7265 722a 277d 0a lorer*'}.
$ xxd crlf.ps1
00000000: 4765 742d 5072 6f63 6573 7320 7c0d 0a57 Get-Process |..W
00000010: 6865 7265 2d4f 626a 6563 7420 7b24 5f2e here-Object {$_.
00000020: 4e61 6d65 202d 494c 696b 6520 272a 6578 Name -ILike '*ex
00000030: 706c 6f72 6572 2a27 7d0d 0a plorer*'}..
(i.e. happens with both, LF and CRLF newlines)
and executing them via -File works as expected:
$ pwsh -File lf.ps1
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
73 41,08 108,07 8,95 3772 4 explorer
$ pwsh -File crlf.ps1
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
73 41,08 108,07 8,95 3772 4 explorer
But If I do the same from stdin:
$ pwsh -Command - <lf.ps1
$ pwsh -Command - <crlf.ps1
there's no output at all and the exit code is still 0.
It does suddenly work, if I add another trailing newline (either LF or CRLF) at the end of the file.
What also works is:
$ pwsh -Command "Get-Process |
Where-Object {\$_.Name -ILike '*explorer*'}"
This is executed from bash, and the string passed as argv to -Command contains a LF newline, i.e. binary it looks like:
$ printf '%s' "Get-Process |
Where-Object {\$_.Name -ILike '*explorer*'}" | xxd
00000000: 4765 742d 5072 6f63 6573 7320 7c20 0a57 Get-Process | .W
00000010: 6865 7265 2d4f 626a 6563 7420 7b24 5f2e here-Object {$_.
00000020: 4e61 6d65 202d 494c 696b 6520 272a 6578 Name -ILike '*ex
00000030: 706c 6f72 6572 2a27 7d plorer*'}
So if the command is given as string, it works even without terminating newline for the last line.
Any ideas why it doesn't work when reading the command from stdin?
btw: I do the this on Windows and invoking Cygwin.
Thanks,
Philippe
Expected behavior
Should execute the command.Actual behavior
Doesn't execute the command, or at least produces no output.Error details
No response
Environment data
$ echo "$PSVersionTable" | pwsh -Command -gives no output either ;-)
### Visuals
_No response_
Activity
philcerf commentedon May 8, 2024
btw: The background to this is that I wanted to be able to start scripts from a shell script like in:
philcerf commentedon May 8, 2024
Oh and I should mention why I titled "(some) commands":
E.g. just
seems to work fine, even without the extr newline before the
END, that makes the above script working.philcerf commentedon May 8, 2024
Just noted, that it doesn't need to be an extra newline to make it work. Seems any even non-newline-terminated character after the newline from the last actual command line makes it work.
For example, the following doesn't work:
while when there's an extra space (or e.g.
\n) after the\nit works:Interestingly, it seems if the extra char is missing, then powershell doesn't even execute the command. Originally I had a quoting error in my code (the
\before the$).In:
No error messages are printed, while in:
there are.
mklement0 commentedon May 9, 2024
As noted in #3223, it is multiline statements that need an extra trailing newline in order to be recognized as a complete statement by the REPL-like behavior exhibited by
-Command -, but the linked issue was aimed at a more fundamental fix: doing away with the REPL behavior, which turned out not to be an option due to backward compatibility.So, thank your opening this a separate issue, which can be dealt with in the context of the by-design REPL behavior.
(I don't have an explanation for why a space in lieu of an extra newline seemingly also works from Bash.)
The extra trailing newline required to terminate a multiline statement appears to be by design (the same behavior applies in an interactive session with
PSReadLineunloaded).The real bug here is that an incomplete statement is ignored; quoting Patrick Meinecke (@SeeminglyScience) from #15331 (comment):
beedell-roke commentedon Nov 13, 2026
chatgpt.com/s/t_6af762bff8d88191921f86a27b0e1454cites this as the reason fordiscourse.jupyter.org/t/39125/3. Might it be?