Contact Info

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


Loop through values in the pipeline


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 } ; $ }