{"id":3967,"date":"2024-05-13T16:51:23","date_gmt":"2024-05-13T16:51:23","guid":{"rendered":"https:\/\/wiki.thomasandsofia.com\/?p=3967"},"modified":"2024-05-13T21:14:16","modified_gmt":"2024-05-13T21:14:16","slug":"powershell-loops","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=3967","title":{"rendered":"Powershell: Loops"},"content":{"rendered":"<h1>Powershell Loops<\/h1>\n<h2>For Loops<\/h2>\n<p><strong>For Loop (Using Counters)<\/strong><\/p>\n<p>For(InitValue; Comparison; Increment){ &#8230; }<\/p>\n<pre>$myFriends = @('Rob', 'Paul', 'Sam', 'Kyle')\r\nFor($counter=0; $counter -le ($myFriends.Length - 1); $counter++){\r\n  Write-Host $myFriends[$counter] \"is my friend.\"\r\n}<\/pre>\n<p><em>Note: Would have been better to use -lt and actual length instead of the extra math, but showing what can be done here. Ex:<\/em> `$counter -lt $myFriends.Length;`<\/p>\n<p><strong>Foreach (Using array elements)<\/strong><\/p>\n<p>Foreach($value in $array){ &#8230; }<\/p>\n<pre>$myFriends = @('Rob', 'Paul', 'Sam', 'Kyle') \r\nForeach($friend in $myFriends){ \r\n  Write-Host $friend \"is my friend.\" \r\n}<\/pre>\n<h2>Do \/ While Loops<\/h2>\n<p><strong>While($condition is true){ &#8230; }<\/strong><\/p>\n<pre>$myFriends = @('Rob', 'Paul', 'Sam', 'Kyle')\r\n$counter = 0\r\nWhile($counter -lt $myFriends.Length){\r\n  Write-Host $myFriends[$counter] \"is my friend.\"\r\n  $counter++\r\n}<\/pre>\n<p><strong>Do &#8211; While Loop<\/strong><\/p>\n<p><em>Will always run at least 1 time<\/em><\/p>\n<pre>$myFriends = @('Rob', 'Paul', 'Sam', 'Kyle')\r\n$counter = 0\r\nDo {\r\n  Write-Host $myFriends[$counter] \"is my friend.\"\r\n  $counter++\r\n} While($counter -lt $myFriends.Length)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Powershell Loops For Loops For Loop (Using Counters) For(InitValue; Comparison; Increment){ &#8230; } $myFriends = @(&#8216;Rob&#8217;, &#8216;Paul&#8217;, &#8216;Sam&#8217;, &#8216;Kyle&#8217;) For($counter=0; $counter -le ($myFriends.Length &#8211; 1); $counter++){ Write-Host $myFriends[$counter] &#8220;is my friend.&#8221; } Note: Would have been better to use -lt and actual length instead of the extra math, but showing what can be done here. ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=3967\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3967","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3967"}],"version-history":[{"count":4,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3967\/revisions"}],"predecessor-version":[{"id":3971,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3967\/revisions\/3971"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}