How to process the output of a command line-by-line as a stream

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:

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…

Source:

How to process the output of a command line-by-line as a stream by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply