Contact Info

Crumbtrail

ActiveXperts.com » Administration » Powershell » Powershell 1.0 » ForEach

ForEach - Powershell 1.0 CmdLet

ActiveXperts Network Monitor ships with integrated Powershell scripts to monitor complex network. The scripts run out of the box
Download the ActiveXperts Network Monitor FREE version now »

ForEach

Description
Loop through values in the pipeline

Usage
ForEach (item in collection) {ScriptBlock}

Options
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.

Example(s)
Loop through a collection of the numbers 1-5, echo each number unless the number is 2:

PS C:>foreach ($num in 1,2,3,4,5) { if ($num -eq 2) { continue } ; $num }

Loop through a collection of .txt files:

PS C:>foreach ($my_file in get-ChildItem *.txt) { if ($my_file.name -eq "dontwant.txt") { continue } ; $my_file.name }