Difference between ForEach and ForEach-Object in powershell
foreach
is an alias of ForEach-Object
but it appears to also be a keyword (which is confusing).
The foreach ($<item> in $<collection\>){<statement list>}
syntax you are using is help about_foreach
.
The foreach
as ForEach-Object
alias is help ForEach-Object
.
The keyword foreach
operates over each $<item>
in the $<collection>
as given in the ()
bit.
The alias foreach
/function ForEach-Object
operates over each item of the collection it receives as input.
They're different commands for different purposes. The ForEach-Object cmdlet is used in the pipeline, and you use either $PSItem or $_ to refer to the current object in order to run a {scriptblock} like so:
1..5 | ForEach-Object {$_}
>1
>2
>3
>4
>5
Now, you can also use a very similiar looking keyword, ForEach, at the beginning of a line. In this case, you can run a {scriptblock} in which you define the variable name, like this:
ForEach ($number in 1..5){$number}
>1
>2
>3
>4
>5
The core difference here is where you use the command, one is used in the midst of a pipeline, while the other starts its own pipeline. In production style scripts, I'd recommend using the ForEach keyword instead of the cmdlet.
Both the previous answers are correct, but https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/08/getting-to-know-foreach-and-foreach-object/ has both a good summary:
When you are piping input into ForEach, it is the alias for ForEach-Object. But when you place ForEach at the beginning of the line, it is a Windows PowerShell statement.
and more details:
The ForEach statement loads all of the items up front into a collection before processing them one at a time. ForEach-Object expects the items to be streamed via the pipeline, thus lowering the memory requirements, but at the same time, taking a performance hit.
He then includes some performance measurements and concludes:
So which one do you use? Well, the answer is, “It depends.” You can iterate through a collection of items by using either the ForEach statement or the ForEach-Object cmdlet. ForEach is perfect if you have plenty of memory, want the best performance, and do not care about passing the output to another command via the pipeline. ForEach-Object (with its aliases % and ForEach) take input from the pipeline. Although it is slower to process everything, it gives you the benefit of Begin, Process, and End blocks. In addition, it allows you to stream the objects to another command via the pipeline. In the end, use the approach that best fits your requirement and the capability of your system.