Tuesday, 4 March 2014

Powershell Pointer: Foreach and Foreach-Object


Foreach
Foreach-Object

Loop

Cmdlet

Used when elements of the array are known previously and are saved in a variable.

Used for parallel processing when elements of the array are not known in advance.


Input cannot be obtained from pipelines.

Usage :  Foreach ($var1 in $var2) { Do something}


Input is received from pipeline only.

Usage :  $var | Foreach-Object {Do something }

Cannot output to a pipeline.

Can write to pipeline

Usage : $var | Foreach-Object {Do something } | Do-SomethingElse