Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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 Server 2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.  However, for older operating systems, installing can be confusing because 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, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save you 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' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of SolarWinds' offer.

Download a free trial of the 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.

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)

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   • Windows PowerShell

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  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-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: