{"id":53803,"date":"2009-05-07T15:20:00","date_gmt":"2009-05-07T15:20:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/05\/07\/hey-scripting-guy-how-can-i-evaluate-a-condition-and-select-from-several-options-with-windows-powershell\/"},"modified":"2009-05-07T15:20:00","modified_gmt":"2009-05-07T15:20:00","slug":"hey-scripting-guy-how-can-i-evaluate-a-condition-and-select-from-several-options-with-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-evaluate-a-condition-and-select-from-several-options-with-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Evaluate a Condition and Select from Several Options with Windows PowerShell?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! I want to evaluate a condition and select from several different options. I know I could use the <B>If\u2026Then\u2026ElseI<\/B><B>f<\/B> to do this, but it is somewhat cumbersome and yesterday you said there was a better way to do these kinds of things. What\u2019s the better way?<BR><BR>&#8211; KM<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hi KM,<\/P>\n<P>You are not going to believe this. But the moment I read your question, the <A href=\"http:\/\/en.wikipedia.org\/wiki\/Little_River_Band\" target=\"_blank\">Little River Band<\/A> started playing on my Zune. I love these guys. They are from Melbourne, Australia, and took their name from a <A href=\"http:\/\/en.wikipedia.org\/wiki\/Little_River,_Victoria\" target=\"_blank\">town in Victoria<\/A>. Melbourne, by the way, is a lovely town. The city has trams that make it easy to move around downtown and It has an <A href=\"http:\/\/en.wikipedia.org\/wiki\/Melbourne_Zoo\" target=\"_blank\">excellent zoo<\/A>. Take a look at the following image, which I took while I was down under teaching a scripting class a few years ago.<\/P><IMG border=\"0\" alt=\"Image of a bridge in Melbourne, Australia\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0507\/hsg-05-07-09-01.jpg\" width=\"500\" height=\"375\"> \n<P>&nbsp;<\/P>\n<P>Anyway, the Little River Band is perhaps best known in the United States for one particular song, &#8220;Help is on its way.&#8221; That\u2019s the same song that started playing on my Zune when I read your e-mail message. The scripting cavalry is here!<\/P>\n<TABLE id=\"EOD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">This week we are looking at scripting Windows PowerShell. Windows PowerShell is installed by default on Windows Server 2008 R2 and Windows 7. It is an optional installation on Windows Server 2008, and a download for <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=c6ef4735-c7de-46a2-997a-ea58fdfcba63\" target=\"_blank\">Windows Vista<\/A>, <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=6ccb7e0d-8f1d-4b97-a397-47bcc8ba3806\" target=\"_blank\">Windows XP<\/A>, and <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=10ee29af-7c3a-4057-8367-c9c1dab6e2bf\" target=\"_blank\">Windows Server 2003<\/A>. The <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" target=\"_blank\">Windows PowerShell Scripting Hub<\/A> is a good place to start using Windows PowerShell. An excellent book for learning Windows PowerShell is the Microsoft Press book, <A href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth10329.aspx\" target=\"_blank\">Microsoft Windows PowerShell Step by Step<\/A>. This book has many hands-on labs and uses real-world examples to show the use of Windows PowerShell.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In VBScript, you would use the <B>Select Case<\/B> statement to evaluate a condition and select one outcome from a group of potential statements. In the <B>DemoSelectCase.vbs<\/B> script, the value of the variable <B>a<\/B> is assigned the value of 2. The <B>Select Case<\/B> statement is used to evaluate the value of the variable <B>a<\/B>. The syntax is seen here:<\/P><PRE class=\"codeSample\">Select Case testexpression\n<\/PRE>\n<P>The test expression that is evaluated is the variable <B>a<\/B>. Each of the different cases contains potential values for the test expression. If the value of the variable <B>a<\/B> is equal to 1, the code <B>Wscript.Echo &#8220;a = 1&#8221;<\/B> is executed. This is seen here:<\/P><PRE class=\"codeSample\">Case 1\n   WScript.Echo &#8220;a = 1&#8221;\n<\/PRE>\n<P>Each of the different cases is evaluated in the same manner. The <B>Case Else<\/B> expression is run if none of the previous expressions evaluate to true. The complete <B>DemoSelectCase.vbs<\/B> script is seen here:<\/P>\n<P><B>DemoSelectCase.vbs<\/B><\/P><PRE class=\"codeSample\">a = 2\nSelect Case a\n Case 1\n   WScript.Echo &#8220;a = 1&#8221;\n Case 2\n   WScript.Echo &#8220;a = 2&#8221;\n Case 3\n   WScript.Echo &#8220;a = 3&#8221;\n Case Else\n   WScript.Echo &#8220;unable to determine value of a&#8221;\nEnd Select\nWScript.Echo &#8220;statement after select case&#8221;\n<\/PRE>\n<P>In Windows PowerShell there is no <B>Select Case<\/B> statement. There is, however, something that is even better: the <B>Switch<\/B> statement. The <B>Switch<\/B> statement is the most powerful statement in the Windows PowerShell language. It is the <A href=\"http:\/\/en.wikipedia.org\/wiki\/Swiss_Army_knife\" target=\"_blank\">Swiss Army knife<\/A> of Windows PowerShell. Let&#8217;s first examine the basic <B>Switch<\/B> statement. The <B>Switch<\/B> statement begins with the <B>Switch<\/B> keyword, and the condition to be evaluated is positioned inside a pair of smooth parentheses. This is seen here:<\/P><PRE class=\"codeSample\">Switch ($a)\n<\/PRE>\n<P>Next, a pair of braces (curly brackets) is used to mark off the script block for the <B>Switch<\/B> statement. Inside the script block, each condition to be evaluated begins with a potential value followed by the script block to be executed in the event the value matches the condition. This is shown here:<\/P><PRE class=\"codeSample\">1 { &#8216;$a = 1&#8217; }\n2 { &#8216;$a = 2&#8217; }\n3 { &#8216;$a = 3&#8217; }\n<\/PRE>\n<P>If no match is found, and the <B>Default<\/B> statement is not used, the <B>Switch<\/B> statement exits and the line of code that follows the <B>Switch<\/B> statement is executed. The <B>Default<\/B> statement performs a function similar to the one performed by the <B>Case Else<\/B> statement from the <B>Select Case<\/B> statement. The <B>Default<\/B> statement is seen here:<\/P><PRE class=\"codeSample\">Default { &#8216;unable to determine value of $a&#8217; }\n<\/PRE>\n<P>The complete <B>DemoSwitchCase.ps1<\/B> script is seen here:<\/P>\n<P><B>DemoSwitchCase.ps1<\/B><\/P><PRE class=\"codeSample\">$a = 2\nSwitch ($a)\n{\n 1 { &#8216;$a = 1&#8217; }\n 2 { &#8216;$a = 2&#8217; }\n 3 { &#8216;$a = 3&#8217; }\n Default { &#8216;unable to determine value of $a&#8217; }\n}\n&#8220;Statement after switch&#8221;\n<\/PRE>\n<P>With the <B>Select Case<\/B> statement, the first matching case is the one that is executed. As soon as that code executes, the line following the <B>Select Case<\/B> statement is executed. If the condition matches multiple cases in the <B>Select Case<\/B> statement, only the first match in the list is executed. In other words, matches from lower in the list are not executed. Therefore, make sure that the most desirable code to execute is positioned highest in the <B>Select Case<\/B> order. <\/P>\n<P>With the <B>Switch<\/B> statement in Windows PowerShell, order is not a major design concern. This is because every match from inside the <B>Switch<\/B> statement will be executed. An example of this is seen in the <B>DemoSwitchMultiMatch.ps1<\/B> script:<\/P>\n<P><B>DemoSwitchMultiMatch.ps1<\/B><\/P><PRE class=\"codeSample\">$a = 2\nSwitch ($a)\n{\n 1 { &#8216;$a = 1&#8217; }\n 2 { &#8216;$a = 2&#8217; }\n 2 { &#8216;Second match of the $a variable&#8217; }\n 3 { &#8216;$a = 3&#8217; }\n Default { &#8216;unable to determine value of $a&#8217; }\n}\n&#8220;Statement after switch&#8221;\n<\/PRE>\n<P>When the <B>DemoSwitchMultiMatch.ps1<\/B> script runs, the second condition and the third condition will both be matched. Therefore, their associated script blocks are executed. The <B>DemoSwitchMultiMatch.ps1<\/B> script produces the output seen here:<\/P><PRE class=\"codeSample\">$a = 2\nSecond match of the $a variable\nStatement after switch\n<\/PRE>\n<P>If an array is stored in the variable <B>a<\/B> in the <B>DemoSelectCase.vbs<\/B> script, a type mismatch error will be produced. This error is seen here: <\/P><PRE class=\"codeSample\">Microsoft VBScript runtime error: Type mismatch\n<\/PRE>\n<P>The Windows PowerShell <B>Switch<\/B> statement can handle an array in the variable <B>$a<\/B> without any modification. The array is seen here:<\/P><PRE class=\"codeSample\">$a = 2,3,5,1,77\n<\/PRE>\n<P>The complete <B>DemoSwitchArray.ps1<\/B> script is seen here:<\/P>\n<P><B>DemoSwitchArray.ps1<\/B><\/P><PRE class=\"codeSample\">$a = 2,3,5,1,77\nSwitch ($a)\n{\n 1 { &#8216;$a = 1&#8217; }\n 2 { &#8216;$a = 2&#8217; }\n 3 { &#8216;$a = 3&#8217; }\n Default { &#8216;unable to determine value of $a&#8217; }\n}\n&#8220;Statement after switch&#8221;\n<\/PRE>\n<P>When the <B>DemoSwitchArray.ps1<\/B> script is run, the results seen in this image are produced:<\/P><IMG border=\"0\" alt=\"Image of multiple matches produced by a Switch statement\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0507\/hsg-05-07-09-02.jpg\" width=\"500\" height=\"131\"> \n<P>&nbsp;<\/P>\n<P>If you do not want the multimatch behavior of the <B>Switch<\/B> statement, you can use the <B>break<\/B> statement to change the behavior. In the <B>DemoSwitchArrayBreak.ps1<\/B> script, the <B>Switch<\/B> statement will be exited when the first match occurs because each of the match condition script blocks contains the <B>break<\/B> statement. This is seen here: <\/P><PRE class=\"codeSample\"> 1 { &#8216;$a = 1&#8217; ; break }\n 2 { &#8216;$a = 2&#8217; ; break }\n 3 { &#8216;$a = 3&#8217; ; break }\n<\/PRE>\n<P>You are not required to include the <B>break<\/B> statement with each condition. Instead, you could use it to exit the <B>Switch<\/B> only after a particular condition is matched. The complete <B>DemoSwitchArrayBreak.ps1<\/B> script is seen here:<\/P>\n<P><B>DemoSwitchArrayBreak.ps1<\/B><\/P><PRE class=\"codeSample\">$a = 2,3,5,1,77\nSwitch ($a)\n{\n 1 { &#8216;$a = 1&#8217; ; break }\n 2 { &#8216;$a = 2&#8217; ; break }\n 3 { &#8216;$a = 3&#8217; ; break }\n Default { &#8216;unable to determine value of $a&#8217; }\n}\n&#8220;Statement after switch&#8221;\n<\/PRE>\n<P>When the <B>DemoSwitchArrayBreak.ps1<\/B> script runs, the output in this image is seen:<\/P><IMG border=\"0\" alt=\"Image of the Switch statement being exited upon first match\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0507\/hsg-05-07-09-03.jpg\" width=\"500\" height=\"113\"> \n<P>&nbsp;<\/P>\n<P>KM, that is the Windows PowerShell <B>Switch<\/B> statement. Today we looked at some of the mundane uses for the <B>Switch<\/B> statement. A more interesting use of the <B>Switch<\/B> statement can be seen in this \u201cHey, Scripting Guy!\u201d article: <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar09\/hey0326.mspx\" target=\"_blank\">How Can I Identify All Local Users, Groups, and Services on a Local Computer?<\/A> In that article, we use regular expressions to do a pattern match; we use the <B>Switch<\/B> statement to maintain a running count of local directory objects. We hope that you have enjoyed this excursion into Windows PowerShell decision-making. Join us tomorrow as we dive into the mailbag and emerge with a handful of queries worthy of Quick-Hits Friday.<\/P>\n<P>&nbsp;<\/P>\n<P><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I want to evaluate a condition and select from several different options. I know I could use the If\u2026Then\u2026ElseIf to do this, but it is somewhat cumbersome and yesterday you said there was a better way to do these kinds of things. What\u2019s the better way?&#8211; KM Hi KM, You are not [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,5,45],"class_list":["post-53803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I want to evaluate a condition and select from several different options. I know I could use the If\u2026Then\u2026ElseIf to do this, but it is somewhat cumbersome and yesterday you said there was a better way to do these kinds of things. What\u2019s the better way?&#8211; KM Hi KM, You are not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53803","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=53803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53803\/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=53803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}