Question:
Writing SomeCommand | { ... }
pipes the entire output of SomeCommand
to the code in the braces. Is there a way to “pipe” one line at a time and let the code in the braces process it line by line, without waiting for the whole output to be stored in memory?
Update
[Copied from comment]
SomeCommand
is an executable (not a powershell cmdlet) that while running produces some output. It takes some time between the lines that it produces, and I do not want to wait until all the lines are produces to perform some operation on each line
Answer:
In order for script block to be able to access pipeline, you need process block inside it:
Compare the two:
1 2 3 4 5 6 7 8 9 10 11 12 |
1, 2, 3 | & { process { $_ Start-Sleep -Seconds 1 } } 1, 2, 3 | & { $input Start-Sleep -Seconds 1 } |
Obviously, Foreach-Object
(mentioned in previous answers) is usually better alternative than using script blocks in situations like this one, but since you asked for script block, script block is what you should also get…