PowerShell Looping: The Continue Statement

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Continue statement in looping.

Microsoft Scripting Guy, Ed Wilson, is here. Dude, dude, dude (or dudette). TechEd 2014 is nearly here. Tomorrow the Scripting Wife and I jump onto a big old hairy plane and head to Houston, Texas for TechEd 2014 North America.

We will head to the Convention Center, and begin the task of assembling the Scripting Guys booth. Look for pictures and ad hoc Hey, Scripting Guy! Blog posts as I try to help you join into the virtual awesomeness that is TechEd. If you are going to be there, make sure you keep up-to-date with the Scripting Guys’ guests, know where to locate the Scripting Guys booth, and even pick up a few session suggestions from the Scripting Wife’s ideal TechEd schedule. It is nearly here!

Understanding the Continue statement

One of the things that is a bit confusing for beginner scripters (and even for some intermediate scripters) is the Continue statement. This is partially because it does not act like it might in some other scripting languages.

Note  Windows PowerShell is not VBScript. Windows PowerShell is not VBScript. Say this 100 times…

So, what does the Continue statement do in Windows PowerShell? It returns flow to the top of the innermost loop that is controlled by a While, For, or Foreach statement. Probably the best way to understand this is to see it in action.

Note  To read more about looping, see these Hey, Scripting Guy! Blog posts.

The following script displays the numbers 1 through 6 as the script works its way through the array.

[array]$a = 1..6

foreach ($i in $a)

{

 $i

}

Here is the output from the script:

Image of command output

If I want to skip the number 3, I can use the If statement to see if the $i variable holds the number 3. If it does, I can use the Continue statement to return to the top of the Foreach loop, and it will continue processing. The revised script is shown here:

[array]$a = 1..6

foreach ($i in $a)

{

 If($i -eq 3){continue}

 $i

}

When I run the script, I see that, indeed, the number 3 is skipped:

Image of command output

Another example using While

I want to show another example of using Continue. This time, I will use the Get-Process cmdlet to store a collection of objects into a variable named $gps. Now I use the GetEnumerator method from the collection to create an enumerator to permit me to walk through the collection one record at a time. I use the While statement to do things while the $e.movenext() method returns True. (By the way, when the MoveNext method moves to the next record, it returns True, which makes it perfect for this example). So while my condition is True, I am going to display the current record. This is the basic script:

$gps = Get-Process

$e = $gps.GetEnumerator()

While ($e.Movenext())

{

    $e.current

}

Here is the output. Pay attention to the portion of the output that displays the Notepad process.

Image of command output

Now I add an If statement to look for the presence of the Notepad process name in the current line of the enumerator. If I find it, then I want to go back to the beginning of the While statement loop, and continue processing:

$gps = Get-Process

$e = $gps.GetEnumerator()

While ($e.Movenext())

{

    if($e.Current.name -match ‘notepad’){Continue}

    $e.current

}

When I run the script, I see that the Notepad process is not present in the output:

Image of command output

That is all there is to using the Continue statement. This also concludes this series about looping. Join me tomorrow for the Weekend Scripter when we will talk about some really cool Windows PowerShell stuff.

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

0 comments

Discussion is closed.

Feedback usabilla icon