PowerShell Continue and Break Statements
Windows PowerShell Continue Explained
PowerShell's 'Continue' statement is employed in loops where we want the script to return to the innermost loop. I also have examples showing how the 'Break' command works.
Topics for PowerShell Continue Statement
I have created a task, or vehicle to test 'Continue', in this scenario I want is to find words, which end in 's', in an array called $Basket. For your information, "s$" refers to 's' in the last character of the string.
The method involves setting-up a ForEach loop, introducing 'If' and 'ElseIf' logic, and finally inserting the Continue or Break command.
Testing the Script
#1: Original Script
#2: Continue is now active
#3: Break is now active
Break will exit the loop after finding the first plural, the ElseIf is not processed, and neither are any more loops; the result is that only 'Apples' gets returned, no Bananas!
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.
Guy's crazy logic - possibly; could do better - almost certainly. Shows the benefit of PowerShell's 'Continue' statement - definitely.
This example contains two loops. The outer loop, $Range contains an array of numbers to check. I calculated the upper limit by raising the largest number in the range (Maximum) to the power of 2, [Math]::pow.
$Known contains an array of known prime numbers, in the course of a loop 'If' tests each $Range number by divided $Item by a prime. If the % (Modulo) is not zero, then that means there is a remainder. At this point in the script 'Continue' plays its part and returns to beginning of the inner loop, without adding that number to the AntiPrime list.
$AntiPrime contains a list of all those numbers that cannot be prime numbers. Next, using Compare-Object we can use a type of subtraction to generate the list of actual PrimeNumbers. Well I warned you - Guy's crazy logic!
# Calculate prime numbers. Features PowerShell Continue
Note 1: Check the effect of removing the word Continue.
Note 2: Much to my surprise, the script generates a list of prime numbers if I replace Continue with Break.
Note 3: Naturally, I am working on ways to improve this script, both in writing tighter code, and in highlighting Continue and Break.
Research PowerShell's Continue and Break
As often, the built-in help files have interesting detail about a command. Although Continue seems like a keyword, PowerShell documentation refers to it as a statement. See more with:
Note 4: Actually, PowerShell's about_Break file was more interesting than the about_Confirm offering. Incidentally, PowerShell has a keyword called 'Return' which is a useful alternative for some scripts.
Summary of PowerShell Continue Statement
PowerShell's 'Continue' statement is useful when you want particular results from loops. It's sister command is 'Break', this returns to the start of the loop with even less processing. Both statements have specialist roles, and help to achieve just the output you want.
If you like this page then please share it with your friends
See more Windows PowerShell flow control examples
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.