The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

mjolinor (5) [Avatar] Offline
In Chapter 19.3 (ForEach), it's implied that you will need to use Foreach-Object instead of a ForEach loop if you want to pipe your output from the loop to another cmdlet. The output from a ForEach loop can easily be made to accomodate the pipeline simply by executing the loop as a scripblock:

.{ForEach ($x in $y){
Do something with $x
} | Get-StuffFromPipeline
Jeffery Hicks (22) [Avatar] Offline
Re: ForEach loops and output to the pipeline.
That is a handy tip, although it certainly adds a level of complexity and isn't something readily apparent to a beginner.
mjolinor (5) [Avatar] Offline
Re: ForEach loops and output to the pipeline.
It's admittedly not something you see done very often (normally you see the result set to a variable, or more fequently an empty array initialized and then results added to that as they are produced within the loop). I didn't quite grasp the way script blocks accumulate objects and then return them to the pipeline until I got to looking into them further. Once I figured that out, other constructs began to make more sense, and using them became more intuitive.

I understand this may be somewhat outside of the intended scope of the book......