title: How does the order of execution of the ForEach-Object cmdlet in a pipeline work?
body: <p>In PowerShell, it appears that the order of execution of cmdlets in a pipeline aren't executed  in an obvious way. Rather than each cmdlet executing then passing the results to the next cmdlet in the pipeline, it seems that individual output objects of a cmdlet are passed to the input of the next cmdlet before the execution of the previous cmdlet complets. The following confirms this behavior:</p>
  
  <p><code>1..5 | %{ Write-Host $_; $_ } | %{ Write-Host ([char]($_ + 64)) }</code> </p>
  
  <p>prints</p>
  
  <pre><code>1
  A
  2
  B
  3
  C
  4
  D
  5
  E
  </code></pre>
  
  <p>It appears what's happening is that each execution of the <code>ForEach-Object</code> cmdlet will execute its script block <strong>and each subsequent command in its pipeline</strong> before iterating.</p>
  
  <p>Is this what actually occurs, and is this behavior documented anywhere? Is this the case for all iterative cmdlets like <code>ForEach-Object</code> (e.g. <code>Where-Object</code>, etc.)? </p>
  
  <p>I know I can wrap pieces in an expression (<code>(1..5 | %{ Write-Host $_; $_ }) | %{ Write-Host ([char]($_ + 64)) }</code>) or assign a piece to a variable and then pipe that to subsequent pipeline commands to avoid this behavior, but is there a way to perform an operation on each element of a collection and then pass that entire collection on to the next command in a pipeline?</p>