ForEach (loop statement)

Loop through a set of input objects and perform an operation (execute a block of statements) against each.

Syntax
ForEach (itemIncollection) {ScriptBlock}
key
item A variable to hold the current item
collection A collection of objects e.g. filenames, registry keys, servernames
ScriptBlock A block of script to run against each object.

The collection will be evaluated and stored as an array in memory before the scriptblock is executed.

The foreach statement does not use pipelining (unlike ForEach-Object ) If you use foreach in a command pipeline PowerShell will actually run the foreach alias that calls Foreach-Object.

Use the ForEach statement when the collection of objects is small enough that it can be loaded into memory.
Use the ForEach-Object cmdlet when you want to pass only one object at a time through the pipeline, minimising memory usage.

ForEach (method)

This method introduced in PowerShell 4.0 and later, provides faster performance than the older foreach statement and ForEach-Objectt cmdlet.