|
Guy recommends :
Find out which of your VMs are a waste of space and which VMs need more resources.
|
PowerShell Loop ExamplesExamples to Illustrate PowerShell LoopingPowerShell 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 ChecklistIn 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 LoopI 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 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 PipeThe 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 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)
|
||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved. Please report a broken link, or an error to: | |