Contact Info

Crumbtrail » 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 »


Loop through values in the pipeline

ForEach (item in collection) {ScriptBlock}

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.

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 ($ -eq "dontwant.txt") { continue } ; $ }