{"id":144,"date":"2019-11-23T18:32:36","date_gmt":"2019-11-23T18:32:36","guid":{"rendered":"http:\/\/192.168.8.14\/?p=144"},"modified":"2020-05-08T18:26:50","modified_gmt":"2020-05-08T17:26:50","slug":"powershell-tutorial-basic-loops","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=144","title":{"rendered":"Powershell tutorial, Basic Loops"},"content":{"rendered":"\n<p>In this PowerShell tutorial I will detail the For loop,  ForEach-Object loop, and the Do loops (Do-While and Do-Until). <\/p>\n\n\n\n<p>These loops are lited below for quick reference (I use the standad for loop so infrequantly I have to normaly look it up) then I will go in to more detail on each one.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n# A bog standard for loop.\nFor ($x=0; $x -le 10; $x++) \n{\n  &quot;loop number $x&quot;\n}\n\n# A foreach loop\nforeach ($Obj in $ObjArray)\n{\n  &quot;This is loop number&quot; + $Obj\n}\n\n# A Do - While Loop\nDo {\n  $SomeThing\n}\nWhile ($SomeThing -eq $true)\n\n# A Do - Until Loop\nDo {\n  $SomeThing\n}\nUntil ($SomeThing -eq $true)\n\n# While Loop\nWhile ($SomeThing -eq $true) {\n  $SomeThing\n}\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"> 1, The standard For loop. <\/h4>\n\n\n\n<p>The first part of the loop syntax is the start value, so in this case $x will have an initial  value of 0.<br>The next part is the logic. ie run the loop code if this statment is true. In this case the loop will keep looping while $x is less or equiel to 10.<br>The last stament is the step. so each time we loop we increace (or decrease) $x by this value.<\/p>\n\n\n\n<p>Examples<br>For ($x = 1; $x -ge -100; $x = $x &#8211; 10) {&#8220;Decrease $x by 10 until we get to -100&#8221;}<br> For ($x = 0; $x -le 100; $x ++) {&#8220;increase $x by 1 until we get to 100&#8221;} <br> For ($x = 30; $x -ge 100; $x = $x + 2) {&#8220;Increase $x by 2 until we get to 100&#8221;} <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> 2, The For each loop.  <\/h4>\n\n\n\n<p>This is the loop I use the most.<br>Loop through each row of an array. Set the variable $Obj to the row of the array Im looping through.<\/p>\n\n\n\n<p>Examples<br>foreach ($Line in $ImportedTextFile){write-host $Line}<br>foreach ($ServiceObj in get-service){ $ServiceObj | select name, status}<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> 3, The Do While loop.   <\/h4>\n\n\n\n<p>In the Do &#8211; While loop we loop through a piece of code while a statment is true. <br>  Note that the evaluation is performed at the end. This means the loop is executed once regardless if the statment is true or not.<\/p>\n\n\n\n<p>Examples<br>$x = 1<br>do{<br>  $x<br>}while($x -le 10) # will keep looping while $x is less then 11.<br>Note: this will be an infinat loop as the value of $x is not changing<br><br>$x = &#8220;EverthingIsFine&#8221;<br>do{<br>  do stuff<br>  if stuff fails set $x to &#8220;OhMyGodStopTheresAnError!!&#8221;<br>}while($x -ne  &#8220;OhMyGodStopTheresAnError!!&#8221; )<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">  4, The Do Until loop.  <\/h4>\n\n\n\n<p>Prety much the same and the Do &#8211; While loop exept this stops looping with then conditinal statment is true. <br>So the above examples can be rewriten as Do &#8211; Until loops as follows:<\/p>\n\n\n\n<p> Examples<br>$x = 1<br>do{<br>  $x<br>}until($x -gt 10) # will keep looping until $x is more then 10.<br>Note: this will be an infinat loop as the value of $x is not changing<br><br> $x = &#8220;EverthingIsFine&#8221;<br>do{<br>  do stuff<br>  if stuff fails set $x to &#8220;OhMyGodStopTheresAnError!!&#8221;<br>}until($x -eq  &#8220;OhMyGodStopTheresAnError!!&#8221; )  <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5, The While loop (no Do)<\/h4>\n\n\n\n<p> This loop is almost the same as the Do &#8211; While loop exept the contition is evaluated at the start of the loop.<br>   Note that now evaluation is performed at the start. This means the loop may not ever run if the evaluation is initially false.<\/p>\n\n\n\n<p> Examples<br>$x = 11<br> while($x -le 10) {<br>  $x<br>} # will still keep looping while $x is less then 11.<br>Note: this will be an infinit loop as the value of $x is not changing, but the loop will never execute in the first place as the While statment will evaluate as false.<br><br> $x = &#8220;IDontKnowIfEverthingIsFine&#8221;<br> while($x -ne &#8220;OhMyGodStopTheresAnError!!&#8221; ) {<br>  do stuff<br>  if stuff fails set $x to &#8220;OhMyGodStopTheresAnError!!&#8221;<br>}  <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this PowerShell tutorial I will detail the For loop, ForEach-Object loop, and the Do loops (Do-While and Do-Until). These loops are lited below for quick reference (I use the standad for loop so infrequantly I have to normaly look it up) then I will go in to more detail on each one. 1, The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,27],"tags":[33],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-powershell","category-tutorial","tag-loops"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=144"}],"version-history":[{"count":1,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}