Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerShell Looping: Basics of the Break

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about the basics of using the Break statement in a Windows PowerShell loop.

Microsoft Scripting Guy, Ed Wilson, is here. Hmmm, it seems that the Scripting Wife and I just returned from the Windows PowerShell Summit, and yet in four days, we are leaving for TechEd in Houston. Oh, wait. We DID just return from the Windows PowerShell summit. Dude, it rocked, too.

We are so looking forward to TechEd. Make sure you stop by the Scripting Guys booth and say "hi." Hey, come hang out at the Scripting Guys booth all week. We have a number of way cool guests who will be stopping by. Check out these posts for more information:

In addition, we will be adding guests all week, so book mark the page, follow @ScriptingGuys on Twitter, and @ScriptingWife on Twitter. We will be sending tweets during TechEd about things as they develop.

We have a couple of pretty cool things to give away. In addition, the folks from PowerShell.org are sharing our booth again this year. It only makes sense, because the community is what makes Windows PowerShell so strong and such a vital piece of technology.

Understanding the Break statement

The Break statement is used to exit a looping statement such as a Foreach, For, While, or Do loop. When present, the Break statement causes Windows PowerShell to exit the loop. The Break statement can also be used in a Switch statement.

Note  For more information about looping in Windows PowerShell script, take a look at these Hey, Scripting Guy! Blog posts.

In the following script, a Do…While loop runs while the value of the $a variable is less than or equal to 5. (This script is discussed in PowerShell Looping: Understanding and Using Do…While.)

$a = 2 #if greater than 5 still runs once

DO

{

 "Starting Loop $a"

 $a

 $a++

 "Now `$a is $a"

} While ($a -le 5)

If the value of $a is less than or equal to 5, the script loops. But if the value is greater than 5, initially, the script runs in an infinite loop. At any rate, the next value displays. This is shown in the following image:

Image of command output

 Using the Break statement

There are two problems with the previous script. The first is that it always displays an extra number, and second is it goes into an infinite loop if the initial value is too great. It would be possible to add structured error handling to fix this problem, but an easier solution is to use the Break statement.

The most serious problem with the script is the infinite loop. To check this problem, I use an If statement, and if the value of $a is greater than my maximum value, I simply use the Break statement inside a script block as shown here:

If($a -gt $max) {break}

The other change I made to the script is to create a value for the maximum value instead of having to hard code it everywhere. And I added a line of script indicating that I was outside of the loop. When the Break statement hits, it exits the loop and goes to the script outside of the loop. It will therefore exit to the “Now do something else outside the loop” statement. Here is the script:

$a = 9 #if greater than 5 if statement catches it

$max = 5

"Enter the loop"

DO

{

 If($a -gt $max) {break}

 "Starting Loop $a"

 $a

 $a++

 "Now `$a is $a"

} While ($a -le $max)

"Now do something else outside the loop"

When I run the script, I see that it catches the condition of a too great value for $a, and therefore avoids the infinite loop. This is shown in the following image:

Image of command output

There is only one problem left, and it is that there is always an extra line printed. It is due to the way the script is written. I display the value of $a after I increment to the new value, but it looks silly when I hit the value of $a = 5 and then it says that $a is equal to 6. This is shown here:

Image of command output

Probably the best thing to do would be to revise the script. But in this example, I don’t want to do that. So instead, I copy my if / break line and place it ahead of the “Now $a is $a” line. The revised script is shown here:

$a = 2 #if greater than 5 if statement catches it

$max = 5

"Enter the loop"

DO

{

 If($a -gt $max) {break}

 "Starting Loop $a"

 $a

 $a++

 If($a -gt $max) {break}

 "Now `$a is $a"

} While ($a -le $max)

"Now do something else outside the loop"

When I run the script now, I no longer get the extra line of text displaying in the output:

Image of command output

That is all there is to using the Break statement within a loop. Windows PowerShell Fundamentals Week will continue tomorrow when I will look at more advanced usage of the Break statement.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles