{"id":1510,"date":"2014-04-28T00:01:00","date_gmt":"2014-04-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/04\/28\/basics-of-powershell-looping-foreach\/"},"modified":"2014-04-28T00:01:00","modified_gmt":"2014-04-28T00:01:00","slug":"basics-of-powershell-looping-foreach","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/basics-of-powershell-looping-foreach\/","title":{"rendered":"Basics of PowerShell Looping: Foreach"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell <strong>Foreach<\/strong> statement to loop through a collection.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. When the Scripting Wife and I were in Amsterdam, Windows PowerShell MVP, Jeff Wouters, told me that a lot of people he ran across had problems looping through collections with Windows PowerShell. &nbsp;Here is a picture of Jeff and me.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1.jpg\" alt=\"Photo of Ed and Jeff\" title=\"Photo of Ed and Jeff\" \/><\/a><\/p>\n<h2>Basics of looping<\/h2>\n<p>Looping is a fundamental Windows PowerShell concept. Actually, I take that back. It is a fundamental concept of any programing language, even batch languages. So what is the problem?<\/p>\n<p>Most people coming to Windows PowerShell for the first time understand about variables. For example, if I store a value in a variable, and if I want to get at that value, it is no problem. I address the variable as shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a = 5<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $b = 6<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $c = 7<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a<\/p>\n<p style=\"margin-left:30px\">5<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $b<\/p>\n<p style=\"margin-left:30px\">6<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $c<\/p>\n<p style=\"margin-left:30px\">7<\/p>\n<p>One thing that makes Windows PowerShell easy to use, is that it automatically unravels arrays. An array is when I add more than one thing to a variable. For example, earlier I assigned three values to three variables. Now, I want to add those three variables to a single variable. So I will use <b>$d<\/b> to hold an array comprised of <b>$a<\/b>, <b>$b<\/b>, and <b>$c<\/b>. In Windows PowerShell, it is easy to see the values. I just call the variable as shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $d = $a,$b,$c<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $d<\/p>\n<p style=\"margin-left:30px\">5<\/p>\n<p style=\"margin-left:30px\">6<\/p>\n<p style=\"margin-left:30px\">7<\/p>\n<p>I can also access the values of the variables by position in the array. The first position is [0], and the last position in our array is [2]. So I can access specific elements from the array by using the position numbers. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $d[0]<\/p>\n<p style=\"margin-left:30px\">5<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $d[1]<\/p>\n<p style=\"margin-left:30px\">6<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $d[2]<\/p>\n<p style=\"margin-left:30px\">7<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h2>Walking through the array<\/h2>\n<p>Suppose I want to add the number five to each of the three values I have in <b>$a<\/b>, <b>$b<\/b>, and <b>$c<\/b>. If I work with them individually, it is easy. I just do the following:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a + 5<\/p>\n<p style=\"margin-left:30px\">10<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $b + 5<\/p>\n<p style=\"margin-left:30px\">11<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $c + 5<\/p>\n<p style=\"margin-left:30px\">12<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>The problem comes with the values I have in my array that is in the <b>$d<\/b> variable. In Windows PowerShell, if I add 5 to my <b>$d<\/b> variable, I end up actually adding the value as another element in the array. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $d + 5<\/p>\n<p style=\"margin-left:30px\">5<\/p>\n<p style=\"margin-left:30px\">6<\/p>\n<p style=\"margin-left:30px\">7<\/p>\n<p style=\"margin-left:30px\">5<\/p>\n<p>To add the number five to each of the elements in the array, I need to walk through the array by using the <strong>Foreach<\/strong> command. To use the <strong>foreach<\/strong> command, I need to do three things:<\/p>\n<ol>\n<li>I call the <strong>Foreach<\/strong> command.<\/li>\n<li>I use a pair of parentheses, I use a variable for my place holder (enumerator), and I use the variable that is holding the collection.<\/li>\n<li>I use a pair of curly braces (script block) that includes the script that does what I want to do.<\/li>\n<\/ol>\n<p>The placeholder variable I use represents the current item from the collection that I will be working with. The variable only gets a value inside the script block, and it will always be a different item each time I loop through the collection. The <strong>Foreach&nbsp;<\/strong>command is shown here:<\/p>\n<p style=\"margin-left:30px\">Foreach (placeholder variable IN collection)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp; What I want to do to the placeholder variable<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>In my example, the <strong>Foreach&nbsp;<\/strong>command is shown here:<\/p>\n<p style=\"margin-left:30px\">Foreach ($i in $d)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$i + 5<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Here&#039;s the entire script:<\/p>\n<p style=\"margin-left:30px\">$a = 5<\/p>\n<p style=\"margin-left:30px\">$b = 6<\/p>\n<p style=\"margin-left:30px\">$c = 7<\/p>\n<p style=\"margin-left:30px\">$d = $a,$b,$c<\/p>\n<p style=\"margin-left:30px\">Foreach ($i in $d)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$i + 5<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>The command and the output from the command are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-28-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-28-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using <strong>Foreach&nbsp;<\/strong>to loop through a collection. Looping Week will continue tomorrow when I will talk about using <b>Foreach-Object<\/b> in the pipeline.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">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 Windows PowerShell Foreach statement to loop through a collection. Microsoft Scripting Guy, Ed Wilson, is here. When the Scripting Wife and I were in Amsterdam, Windows PowerShell MVP, Jeff Wouters, told me that a lot of people he ran across had problems looping through collections [&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-1510","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 Windows PowerShell Foreach statement to loop through a collection. Microsoft Scripting Guy, Ed Wilson, is here. When the Scripting Wife and I were in Amsterdam, Windows PowerShell MVP, Jeff Wouters, told me that a lot of people he ran across had problems looping through collections [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1510","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=1510"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1510\/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=1510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}