How it works...
The ForEach-Object cmdlet, the Foreach method, and the foreach statement are all used to iterate over collections of objects. It doesn't matter if the collection that is used is empty or just contains one single element. With an empty collection, no iteration will happen. A single element will only trigger a single iteration.
The Begin, Process, and End blocks that you have used in this recipe are incidentally also used in pipeline cmdlets for the very same purpose.
Using the cmdlet will always include a slight decrease in performance. In case a large collection will be processed, the foreach method or the foreach statement will be a better fit. Take a look at this code sample and the time it took to process a simple collection ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access