{"id":17834,"date":"2008-11-19T07:56:48","date_gmt":"2008-11-19T15:56:48","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/powershell\/?p=17834"},"modified":"2019-06-07T07:58:10","modified_gmt":"2019-06-07T15:58:10","slug":"step-up-your-foreach-game","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/step-up-your-foreach-game\/","title":{"rendered":"Step up your FOREACH Game"},"content":{"rendered":"<p>Last night Bruce Payette and I were interviewed on the <a href=\"http:\/\/powerscripting.wordpress.com\/\">PowerScripting Podcast<\/a> with Hal Rottenberg and Jonathan Walz.&#160; It was a blast!&#160; There were a ton of people connected and were firehosing questions &amp; comments in the Chat window.&#160; There were lots of comments\/discussions about Twitter.&#160; I confessed to having never used twitter.&#160; <\/p>\n<p>This morning I got an email from <a href=\"http:\/\/blog.sapien.com\/\">Jeffery Hicks<\/a> recommended a site called TweetGrid which lets you &quot;monitor the tweet stream&quot; without a Twitter account.&#160; (If I get an account, do I become a Twit?)&#160; He sent me <a href=\"http:\/\/tweetgrid.com\/grid?l=1&amp;q1=powershell&amp;q2=CTP3\">a link to a page with search panels for PowerShell and CTP3<\/a>.<\/p>\n<p>I&#8217;m not ready to recommend it yet BUT the very first item on the bullet was from depping with a <a href=\"http:\/\/www.peetersonline.nl\/index.php\/vmware\/add-rdm-size-info-to-vi-client-using-powershell\/\">link to a blog post by&#160; Hugo Peeters about script that adds RDM size info to a VI client using PowerShell<\/a>.&#160; I love looking at other peoples scripts &#8211; it gives me a view into how they think about the world and helps me understand what patterns are in use and gives me ideas about how to evolve the language and engine.<\/p>\n<p>Hugo wrote a nice script &#8211; it is very easy to read and maintain.&#160; One thing caught my eye and I left him a note about it.&#160; He wrote:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">$VMs = Get-VM      <br \/>ForEach ($VM in $VMs)<\/font><\/p>\n<p>This is perfectly fine code &#8211; it is the traditional way foreach loops are used.&#160; But foreach loops in PowerShell are much more than that.&#160; I&#8217;ve found that wherever I had code like that, I&#8217;m now converting it to the following form:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">Foreach ($VM in Get-VM)<\/font><\/p>\n<p>&#160;<\/p>\n<p>In PowerShell, the COLLECTION part of the statement can be ANYTHING EXPRESSION OR <strong>STATEMENT<\/strong> THAT GENERATES A COLLECTION.&#160; So you can use statements in your foreach.&#160; I don&#8217;t have VMWARE so I can give you good VM examples but here are some examples of what I mean<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">foreach ($p in Get-Process *ss)      <br \/>{ $p.Name}      <br \/># A cmdlet is a statement which generates a collection.&#160; Don&#8217;t worry if it      <br \/># only generates 1 value &#8211; PowerShell casts it to a collection<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\"><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">foreach ($p in Get-Process |where {$_.handles -ge 500})      <br \/>{ $p.Name}      <br \/># A pipeline is a statement as well<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\"><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">foreach ($p in Get-Process |where {$_.handles -ge 500} | sort handles)      <br \/>{ $p.Name}      <br \/># There is no restrictions on what you do in&#160; your statement<\/font><\/p>\n<p>&#160;<\/p>\n<p>If&#160; you want to do something which is multiple statements, you can put them inside a $() and they are considered a single statement.   <br \/><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">foreach ($p in $($max=700; gps |where {$_.handles -le $max})) {$p.Name}<\/font><\/p>\n<p>&#160;<\/p>\n<p>Here is why I like this usage of foreach:<\/p>\n<ol>\n<li>It is more readable\/maintainable.<\/li>\n<li>It is smaller.&#160; IF the code is readable, smaller code is generally better.&#160; There are fewer things to keep track of and fewer things that can go wrong.<\/li>\n<li>I don&#8217;t have to think up a good variable name.<\/li>\n<li>I get a charge of the fact that you can do this in PowerShell (not a good reason but it&#8217;s honest. \ud83d\ude42 ).<\/li>\n<\/ol>\n<p>Thanks for the nice script Hugo!<\/p>\n<p>Thanks for the recommendation Jeffrey!<\/p>\n<p>Thanks for a great podcast Hal and&#160; Jonathan!<\/p>\n<p>Cheers!<\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Windows Management Partner Architect    <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>    <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last night Bruce Payette and I were interviewed on the PowerScripting Podcast with Hal Rottenberg and Jonathan Walz.&#160; It was a blast!&#160; There were a ton of people connected and were firehosing questions &amp; comments in the Chat window.&#160; There were lots of comments\/discussions about Twitter.&#160; I confessed to having never used twitter.&#160; This morning [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[192,203,247],"class_list":["post-17834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-hal-rottenberg","tag-interview","tag-powerscriptingpodcast"],"acf":[],"blog_post_summary":"<p>Last night Bruce Payette and I were interviewed on the PowerScripting Podcast with Hal Rottenberg and Jonathan Walz.&#160; It was a blast!&#160; There were a ton of people connected and were firehosing questions &amp; comments in the Chat window.&#160; There were lots of comments\/discussions about Twitter.&#160; I confessed to having never used twitter.&#160; This morning [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=17834"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17834\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=17834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=17834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=17834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}