In the case of the
Foreach-Object,
the statement body is executed as soon as each object is produced. In the foreach statement, all the objects are collected before the loop body begins to execute. This has two implications

First, because in the
foreach statement case all the objects are gathered at once, you need to have enough memory to hold all these objects. In
the Foreach-Object case, only one object is read at a time so less storage is required. From this, you would think that
Foreach-Object should always be preferred. In the bulk-read case, however, there are some optimizations that the
foreach statement does that allow it to perform significantly faster than the
Foreach-Object cmdlet. The result is a classic speed versus space tradeoff. In practice, though, you rarely need to consider
these issues, so use whichever seems most appropriate to the solution at hand.

The second difference is that in the
Foreach-Object case, the execution of the pipeline element generating the object is interleaved with the execution of the
Foreach-Object cmdlet. In other words, the command generates one object at a time and then passes it to
foreach for processing before generating the next element. This means that the statement list can affect how subsequent pipeline
input objects are generated. Unlike traditional shells where each command is run in a separate process and can therefore actually run at the same time, in PowerShell they’re
alternating— the command on the left side runs and produces an object, and then the command on the right side runs.
Executing the
foreach statement also defines a special variable for the duration of the loop. This is the
$foreach variable and it’s bound to the loop enumerator. (The
foreach statement keeps track of where it is in the collection through the loop enumerator.) By manipulating the loop enumerator,
you can skip forward in the loop.

In the case of the
Foreach-Object,
the statement body is executed as soon as each object is produced. In the foreach statement, all the objects are collected before the loop body begins to execute. This has two implications

First, because in the
foreach statement case all the objects are gathered at once, you need to have enough memory to hold all these objects. In
the Foreach-Object case, only one object is read at a time so less storage is required. From this, you would think that
Foreach-Object should always be preferred. In the bulk-read case, however, there are some optimizations that the
foreach statement does that allow it to perform significantly faster than the
Foreach-Object cmdlet. The result is a classic speed versus space tradeoff. In practice, though, you rarely need to consider
these issues, so use whichever seems most appropriate to the solution at hand.

The second difference is that in the
Foreach-Object case, the execution of the pipeline element generating the object is interleaved with the execution of the
Foreach-Object cmdlet. In other words, the command generates one object at a time and then passes it to
foreach for processing before generating the next element. This means that the statement list can affect how subsequent pipeline
input objects are generated. Unlike traditional shells where each command is run in a separate process and can therefore actually run at the same time, in PowerShell they’re
alternating— the command on the left side runs and produces an object, and then the command on the right side runs.
Executing the
foreach statement also defines a special variable for the duration of the loop. This is the
$foreach variable and it’s bound to the loop enumerator. (The
foreach statement keeps track of where it is in the collection through the loop enumerator.) By manipulating the loop enumerator,
you can skip forward in the loop.

Thanks, Ketan. Those articles really cleared it up for me: It's basically memory vs. performance, and I suppose if your know you collection of objects is going to take a lot of memory, it's probably best to use Foreach-Object.

But for performance purposes, foreach (at the beginning of a line) is
much faster. Try these two tests:

Another difference I noticed between Foreach loop and Foreach-Object is, their ability to support loop control statements like break and continue. While these two statements work as expected with foreach loop, they don't have any significance when used inside
Foreach-Object. See http://techibee.com/powershell/difference-between-foreach-loop-and-foreach-object-in-powershell/2164 for a few examples about this.

Microsoft is conducting an online survey to understand your opinion of the Technet Web site. If you choose to participate, the online survey will be presented to you when you leave the Technet Web site.