Powershell fails to run multi-line commands from stdin? Powershell fails to run multi-line commands from stdin? powershell powershell

Powershell fails to run multi-line commands from stdin?


I'm going to consider this answered by this: How to end a multi-line command in PowerShell since it shows that an extra newline is required. However, I'm going to raise this to MS as a bug since this should not be required when reading from a non-tty, or when -NonInteractive switch is specified.

Please vote on my bug report to the Powershell team.


This is not a complete answer, but from what I can tell, the problem has to do with the input being sent in line by line.

To demonstrate the line-by-line issue, I invoke powershell this way:

powershell.exe -command "gc myfile.txt" | powershell.exe -

vs

powershell.exe -command "gc myfile.txt -raw" | powershell.exe -

The first example replicates what you see with type, the second reads the entire contents of the file, and it works as expected.

It also works from within PowerShell if you put the script contents in a string and pipe it into powershell.exe -.

I had a theory that it had to do with line-by-line input lacking line breaks, but it's not so clear cut. If that were the case, why would the first option work but not the second (removing the line break splitting the single pipeline should have no effect, while removing the line break between the pipeline and the echo should make it fail). Maybe there's something unclear about the way powershell is handling the input with or without line breaks.