PowerShell/Loops

This lesson introduces PowerShell loops, including condition-based while and do while/do until loops, counter-based for loops, and collection-based foreach loops.

Objectives and Skills
After completing this lesson, you will be able to:
 * Describe basic loop statement concepts.
 * Create PowerShell scripts that use while and do loops.
 * Create PowerShell scripts that use for loops.
 * Create PowerShell scripts using foreach loops.

Readings

 * 1)  Control flow
 * 2) BonusBits: Mastering PowerShell Chapter 8 - Loops

Multimedia

 * 1) YouTube: PowerShell For Loops
 * 2) YouTube: Do While Loops in PowerShell
 * 3) YouTube: ForEach Loops in PowerShell
 * 4) YouTube: PowerShell Loops
 * 5) YouTube: PowerShell - How To - Loops

While
The while statement runs a statement list zero or more times based on the results of a conditional test.

Do While
The do while statement runs a statement list one or more times based on the affirmative results of a conditional test.

It is important to note that, unlike while, a do while or do until will always execute at least once.

Do Until
The do until statement runs a statement list one or more times based on the negative results of a conditional test.

For
The for statement runs a statement list zero or more times based on an initial setting, a conditional test, and a repeated statement, most often used with numeric counters.

For loops are often nested to repeat actions, such as for rows and columns. For example:

ForEach
The foreach statement runs a statement list once for each item in a collection.

Continue
The continue statement immediately returns script flow to the top of the innermost While, Do, For, or ForEach loop.

Break
The break statement causes Windows PowerShell to immediately exit the innermost While, Do, For, or ForEach loop or Switch code block.

Exit
Exit causes Windows PowerShell to exit a script or a Windows PowerShell instance.

Foreach-Object
The ForEach-Object cmdlet runs a command list once for each item in a collection. While the ForEach-Object cmdlet is often more convenient, the foreach statement and a coded loop usually offers better performance.

Start-Sleep
The Start-Sleep cmdlet allows you to pause Windows PowerShell activity for a specified period of time. It is particularly useful with long-running scripts or infinite loops.

Get-Counter
The Get-Counter cmdlet gets performance counter data from local and remote computers.

Activities

 * 1) Create a script that asks the user to enter grade scores.  Start by asking the user how many scores they would like to enter.  Then use a loop to request each score and add it to a total.  Finally, calculate and display the average for the entered scores.  Revise the script several times to compare the code required when the loop control structure is based on while, do while, do until, and for statements, respectively.
 * 2) Review MathsIsFun: 10x Printable Multiplication Table.  Create a script that uses nested for loops to generate a multiplication table.  Rather than simply creating a 10 by 10 table, ask the user to enter the starting and ending values.
 * 3) Review MathsIsFun: 10x Printable Multiplication Table.  Create a script that uses nested for loops to generate a multiplication table.  Use a condition with ( % 2) to test for odd or even values.  Generate the multiplication table only for even values.  For odd values, use the continue statement to continue the next iteration of the loop.
 * 4) Review MathsIsFun: 10x Printable Multiplication Table.  Create a script that uses nested for loops to generate a multiplication table.  Rather than simply creating a 10 by 10 table, ask the user to enter the starting and ending values.  Use a break statement to terminate the loop if the count exceeds 10.
 * 5) Create a script that retrieves a list of all services and then uses foreach to display the names of all running services.  While this could also be done using Select-Object, there are times when processing objects is faster and easier using foreach.
 * 6) Review Microsoft TechNet: Using the Start-Sleep Cmdlet.  Write a script that uses an infinite loop to call the Get-Counter cmdlet every sixty seconds.  Redirect the Get-Counter output and append it to a file.  Run the script for a few minutes and then check the file contents to observe system utilization.

Lesson Summary

 * Control flow refers to the order in which the individual statements, instructions or function calls of an imperative or a declarative program are executed or evaluated.
 * Control flow statement types include unconditional branch, conditional branch, conditional loop, subroutines, and unconditional halt.
 * PowerShell does not support an unconditional branch. All control flow must be structured using conditions, loops, functions (subroutines), or exit (unconditional halt).
 * PowerShell uses curly braces to designate code blocks and control structures.
 * A loop is a sequence of statements which is specified once but which may be carried out several times in succession.
 * Loop structures include while and do while/do until, for, and foreach.
 * While and do while/do until are condition-controlled loops, repeating until some condition changes.
 * For loops are count-controlled loops, repeating a certain number of times.
 * Foreach loops are collection-controlled loops repeating for all elements of an array, or all members of a set or collection.
 * Loops may be continued prematurely using the continue statement.
 * Loops may be terminated prematurely using the break statement.
 * Scripts may be terminated prematurely using the exit statement.
 * The ForEach-Object cmdlet runs a command list once for each item in a collection.
 * The Start-Sleep cmdlet allows you to pause Windows PowerShell activity for a specified period of time.
 * The Get-Counter cmdlet gets performance counter data from local and remote computers.

Key Terms

 * infinite loop
 * A sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over.

Assessments

 * Flashcards: Quizlet: Windows PowerShell - Loops
 * Quiz: Quizlet: Windows PowerShell - Loops