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

In order for script block to be able to access pipeline, you need process block inside it: Compare the two:

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...


In powershell, objects are passed item-by-item along the object pipeline.

A related useful characteristic of pipelines is that because they operate on each item separately, you do not have to modify them based on whether you will have zero, one, or many items in the pipeline. Furthermore, each command in a pipeline (called a pipeline element) usually passes its output to the next command in the pipeline item-by-item. This usually reduces the resource demand of complex commands and allows you to begin getting the output immediately.

From Technet

There are some exceptions, though. Some commands (like Sort-Object), need all input objects to be able to send the result to the next step in the object pipeline.

In your example, You could use the ForEach-Object cmdlet (aliased as %) and use its special $_ variable which represents the current item being processed:

Get-ChildItem $pshome | ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; "" }}

EDIT 2 Since you are calling an executable, powershell will split stdout into lines as it receives them and send each string down the object pipeline. See the answer by @use.


Pipe the output from the (old-style) text stream executable through PowerShell's foreach cmdlet:

ping.exe www.somesite.com | %{$_}

Explanation: % is an alias for Foreach-Object. It cuts up a text stream at the newline characters and emits the lines one after another. Actually, most cmdlets that takes strings as pipe input will just work on the command line (it is actually PowerShell that cuts up the text stream, and not the cmdlets).

Tags:

Powershell