{"id":1453,"date":"2014-05-09T00:01:00","date_gmt":"2014-05-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/09\/powershell-looping-the-continue-statement\/"},"modified":"2022-06-20T13:15:57","modified_gmt":"2022-06-20T20:15:57","slug":"powershell-looping-the-continue-statement","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-the-continue-statement\/","title":{"rendered":"PowerShell Looping: The Continue Statement"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about using the <strong>Continue<\/strong> statement in looping.<\/p>\n<p>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.<\/p>\n<p>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 <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/scripting-guys-booth-guest-meet-and-greet-schedule-for-teched-2014\/\" target=\"_blank\" rel=\"noopener\">up-to-date with the Scripting Guys&#8217; guests<\/a>, know where to <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-find-the-scripting-guys-booth-at-teched-2014\/\" target=\"_blank\" rel=\"noopener\">locate the Scripting Guys booth<\/a>, and even pick up a few session suggestions from the <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/scripting-wife-creates-ideal-teched-2014-schedule\/\" target=\"_blank\" rel=\"noopener\">Scripting Wife\u2019s ideal TechEd schedule<\/a>. It is nearly here!<\/p>\n<h2>Understanding the Continue statement<\/h2>\n<p>One of the things that is a bit confusing for beginner scripters (and even for some intermediate scripters) is the <strong>Continue<\/strong> statement. This is partially because it does not act like it might in some other scripting languages.<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note\u00a0<\/b> Windows PowerShell is not VBScript. Windows PowerShell is not VBScript. Say this 100 times\u2026\n<\/p>\n<p>So, what does the <strong>Continue<\/strong> statement do in Windows PowerShell? It returns flow to the top of the innermost loop that is controlled by a <strong>While<\/strong>, <strong>For<\/strong>, or <strong>Foreach<\/strong> statement. Probably the best way to understand this is to see it in action.<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note<\/b>\u00a0\u00a0To read more about looping, see these <a href=\"\/b\/heyscriptingguy\/archive\/tags\/looping\/\" target=\"_blank\" rel=\"noopener\">Hey, Scripting Guy! Blog posts<\/a>.\n<\/p>\n<p>The following script displays the numbers 1 through 6 as the script works its way through the array.<\/p>\n<p style=\"margin-left:30px\">\n  [array]$a = 1..6\n<\/p>\n<p style=\"margin-left:30px\">\n  foreach ($i in $a)\n<\/p>\n<p style=\"margin-left:30px\">\n  {\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$i\n<\/p>\n<p style=\"margin-left:30px\">\n  }\n<\/p>\n<p>Here is the output from the script:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I want to skip the number 3, I can use the <strong>If<\/strong> statement to see if the <strong>$i<\/strong> variable holds the number 3. If it does, I can use the <strong>Continue<\/strong> statement to return to the top of the <strong>Foreach<\/strong> loop, and it will continue processing. The revised script is shown here:<\/p>\n<p style=\"margin-left:30px\">\n  [array]$a = 1..6\n<\/p>\n<p style=\"margin-left:30px\">\n  foreach ($i in $a)\n<\/p>\n<p style=\"margin-left:30px\">\n  {\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0If($i -eq 3){continue}\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$i\n<\/p>\n<p style=\"margin-left:30px\">\n  }\n<\/p>\n<p>When I run the script, I see that, indeed, the number 3 is skipped:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Another example using While<\/h2>\n<p>I want to show another example of using <strong>Continue<\/strong>. This time, I will use the <strong>Get-Process<\/strong> cmdlet to store a collection of objects into a variable named <strong>$gps<\/strong>. Now I use the <strong>GetEnumerator<\/strong> method from the collection to create an enumerator to permit me to walk through the collection one record at a time. I use the <strong>While<\/strong> statement to do things while the <strong>$e.movenext()<\/strong> method returns True. (By the way, when the <strong>MoveNext<\/strong> 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:<\/p>\n<p style=\"margin-left:30px\">\n  $gps = Get-Process\n<\/p>\n<p style=\"margin-left:30px\">\n  $e = $gps.GetEnumerator()\n<\/p>\n<p style=\"margin-left:30px\">\n  While ($e.Movenext())\n<\/p>\n<p style=\"margin-left:30px\">\n  {\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 $e.current\n<\/p>\n<p style=\"margin-left:30px\">\n  }\n<\/p>\n<p>Here is the output. Pay attention to the portion of the output that displays the Notepad process.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now I add an <strong>If<\/strong> 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 <strong>While<\/strong> statement loop, and continue processing:<\/p>\n<p style=\"margin-left:30px\">\n  $gps = Get-Process\n<\/p>\n<p style=\"margin-left:30px\">\n  $e = $gps.GetEnumerator()\n<\/p>\n<p style=\"margin-left:30px\">\n  While ($e.Movenext())\n<\/p>\n<p style=\"margin-left:30px\">\n  {\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 if($e.Current.name -match &#8216;notepad&#8217;){Continue}\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 $e.current\n<\/p>\n<p style=\"margin-left:30px\">\n  }\n<\/p>\n<p>When I run the script, I see that the Notepad process is not present in the output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-9-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using the <strong>Continue<\/strong> 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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,95,3,4,45],"class_list":["post-1453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-looping","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=1453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=1453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}