I've had trouble grasping how the PowerShell pipeline works and I realise a lot of the problem is due to ForEach-Object. In other languages I've used, foreach operates on a collection, iterating through each element of the collection in turn. I assumed ForEach-Object, when used in a PowerShell pipeline, would do the same. However, everything I read about the pipeline suggests each element of a collection is passed through the pipeline separately and that downstream cmdlets are called repeatedly, operating on each element separately rather than on the collection as a whole.
So does ForEach-Object operate on a single element in the collection, rather than on the collection as a whole? Looking at it a different way, does the pipeline operator pass through the whole collection to ForEach-Object, which then iterates over it, or does the pipeline object iterate over the collection and pass each element separately to ForEach-Object?