PowerShell: Return, ForEach,ForEach-Object and a pipe

I just figured this out late last night. I was pulling my hair out. If you look at the following PowerShell code

($MyArray=1,2,3,4,5) | foreach-object {
write-host $_
if ($_ -eq 3 ){return}
}

You get a result of:
1
2
3
4
5

Next, if you look at this code (the only real difference being the lack of a pipe.)

foreach ($item in ($MyArray=1,2,3,4,5)) {
write-host $item
if ($item -eq 3 ){return}
}

You get:
1
2
3

That stumped me. Why would they not produce the same thing? Then I ran across this thread. The replier said

I think because ForEach-Object processes each item in the pipeline, but the foreach statement processes the collection as a whole.

This led me to this page, and specifically this quote clears it up for me (kinda):

 . . . [the object] it is sent into the pipeline and execution continues in the next section of the pipeline.  . . . the foreach alias gets executed and the object is run through the process script block of ForEach-Object.  Once the process script block completes, the object is discarded and the next object is [sent] . . .

In summary, last night I learned that when using a pipeline, the first item is passed to the script block, the script block is run, and then the next item is passed to the script block, and so on.

That sound correct? Can you come up with a better description?

2 Responses to PowerShell: Return, ForEach,ForEach-Object and a pipe

  1. Stephan Samuel March 21, 2012 at 11:47 am #

    This is called, “lazy evaluation.” By using foreach, you’re deferring processing of each building block until such a time as it’s required.

    The type of operations that force the hand of execution are: sorting, counting, min/max, etc. These operations require knowledge of the entire set. That implies — with intention — that for a large part, piped processing doesn’t know what’s in the set until it hits a point where it needs to.

Trackbacks/Pingbacks

  1. PowerShell ForEach und ForEach-Object - September 9, 2013

    […] Dieser Post ist eine (verbesserte) Übersetzung von dem guten Blog Post von Kirk Munro Essential PowerShell: Understanding foreach http://poshoholic.com/2007/08/21/essential-powershell-understanding-foreach/ Essential PowerShell: Understanding foreach (Addendum) http://poshoholic.com/2007/08/31/essential-powershell-understanding-foreach-addendum/ Sehr tiefgehende ForEach Performanz Diskussion im Blog von Brendon Shell: Why use foreach vs foreach-object. http://bsonposh.com/archives/327 PowerShell: Return, ForEach,ForEach-Object and a pipe http://www.jbmurphy.com/2011/11/29/powershell-return-foreachforeach-object-and-a-pipe/ […]