Contact Info

Crumbtrail

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

ForEach - Powershell 1.0 CmdLet

Microsoft Windows PowerShell is a command-line shell and scripting tool based on the Microsoft .NET Framework. It is designed for system administrators, engineers and developers to control and automate the administration of Windows and applications.

More than hundred command-line tools (so called "cmdlets") can be used to perform system administration tasks and Windows Management Instrumentation (WMI). These cmdlets are easy to use, with standard naming conventions and common parameters, and standard tools for piping, sorting, filtering, and formatting data and objects.

ForEach

Description
Loop through values in the pipeline

Usage


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 }