PowerShell Loop Examples

Examples to Illustrate PowerShell Looping

PowerShell provides a rich variety of looping techniques for applying a block statement to a series of items.  To automate a repetitive task learn the syntax for a 'Foreach' or 'While' loop.

Basic PowerShell Examples

 ♣

PowerShell Pre-requisites and Checklist

In the case of Windows 7 and later, you don't need to download any extra files, just: 'Add Feature' --> Windows PowerShell.  However, for older operating systems, there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  For such legacy systems only, you need to download PowerShell from Microsoft's site.

Once you have installed PowerShell 2.0 or later, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save buying a text editor.

Example 1: Classic PowerShell Loop

I find the PowerShell 'Foreach' loops versatile and it will serve you well once you grasp the rhythm of its syntax.  Let us start with an example to calculate thirteen times table.

# Example of PowerShell Loop
$NumArray = (1,2,3,4,5,6,7,8,9,10,11,12)
Foreach ($Item in $NumArray) {$Item * 13}

Note 1:  It pays to study the two sets of brackets.  The ($Parenthesis) style introduce the variable, then thanks to the tiny word 'in' Foreach loops through an array.  The second set of {Curly} brackets contains the payload or the block statement.

Example 2: Foreach Loop with Pipe

The crucial difference between example 1 and 2 is that in this second example foreach accepts input from PowerShell's signature tune the | (pipe).

# Example of PowerShell Loop with Piped Input
Get-Service | Foreach {$_.name + " Status:-" + $_.status}

Note 2:  Unlike example 1, here there are no parenthesis brackets and there is no 'in'.  The loop works because Foreach accepts input from Get-Service then applies the block statement of name and status to each item.

See more on PowerShell's $_ variable »

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

Example 3: PowerShell While Loop

As with Foreach, the types of bracket are highly significant in the 'While' loop.  The difference is that in the While loop the (parenthesis contain the condition).

# PowerShell While Loop
$i =8
While ($i -le 96) {$i; $i +=8}

Note 3: The key point is the condition in the first bracket, the variable is less than 96.  While that is true the payload in the second set of brackets executes.  Observe how a straightforward scripting technique adds 8 to the variable during each successful loop.

Example 4: Do Until

There is a variation of this style of loop, 'Do .. Until'.  The layout of the components is the same as 'Do While', the only difference is that the logic is changed from, do while condition is true, to, do until condition is true.

$i = 7; do { $i; $i +=7 } until ($i -gt 85)

See more on PowerShell's loops »

Summary of PowerShell Loops

Automating repetitive tasks is what scripting is all about.  Therefore, each scripting language needs at least one method for cycling, or looping through a block of instructions.  PowerShell provides a several techniques notably, 'Foreach' and While.

If you like this page then please share it with your friends

 


See more Windows PowerShell tutorials

PowerShell Tutorials  • PowerShell Examples  • PowerShell Service  • PowerShell Loops

Process Example  • PowerShell services  • Get-Member  • PowerShell Home   • Windows PowerShell

Top 10 Commands to Try in PowerShell  • PowerShell v3   • Free CSV Import Tool

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: