{"id":53813,"date":"2009-05-06T15:19:00","date_gmt":"2009-05-06T15:19:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/05\/06\/hey-scripting-guy-how-can-i-use-the-if-statement-in-windows-powershell\/"},"modified":"2009-05-06T15:19:00","modified_gmt":"2009-05-06T15:19:00","slug":"hey-scripting-guy-how-can-i-use-the-if-statement-in-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-the-if-statement-in-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use the If Statement in 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 have a problem with a script I am trying to write. I have to evaluate between several conditions as they come into the script, but I do not understand the way the <B>If<\/B> statement in Windows PowerShell works. I even tried to use the <B>Get-Help<\/B> cmdlet to find Help, but when I use <B>Get-Help If<\/B>, it says it cannot find Help for that topic. Surely Windows PowerShell contains an <B>If<\/B> statement. One good thing is the error messages in Windows PowerShell point you in the appropriate direction. I tried several things, and finally got the statement to work. However, it is now giving me bogus results. I\u2019m attaching a picture. Can you help me figure it out?<BR><BR>&#8211;DZ<\/P><IMG border=\"0\" alt=\"Image of trial-end-error attempts to make If statement work\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0506\/hsg-05-06-0901.jpg\" width=\"500\" height=\"320\"> \n<P>&nbsp;<\/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 DZ,<\/P>\n<P>I will give you credit for persistence! In Windows PowerShell if you want to use <B>Get-Help<\/B> to learn about something, look for an &#8220;about topic&#8221;. Interestingly, if you were using Windows PowerShell 2.0 (currently in <A href=\"http:\/\/blogs.msdn.com\/powershell\/pages\/download-windows-powershell.aspx\" target=\"_blank\">CTP 3<\/A>), your first query would have worked and you would have found the <B>about_if<\/B> topic. Here is the Help query that you should have used:<\/P><PRE class=\"codeSample\">Get-Help about_if\n<\/PRE>\n<P>In Windows PowerShell 2.0, the <B>Get-Help<\/B> cmdlet uses a regular expression match to find Help topics. As seen here, both cmdlet Help and about topic Help are searched.<\/P><PRE class=\"codeSample\">PS C:\\&gt; Get-Help if<\/p>\n<p>Name                              Category  Synopsis\n&#8212;-                              &#8212;&#8212;&#8211;  &#8212;&#8212;&#8211;\nNew-ModuleManifest                Cmdlet    Creates a new module manifest.\nTest-ModuleManifest               Cmdlet    Verifies that a module manifest &#8230;\nGet-PfxCertificate                Cmdlet    Gets information about .pfx cert&#8230;\nabout_if                          HelpFile  Describes a language command you&#8230;\n<\/PRE>\n<TABLE id=\"EZD\" 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 with 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 the <B>If\u2026Then\u2026End If<\/B> statement was somewhat straightforward. There were several things to be aware of:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The <B>If<\/B> and the <B>Then<\/B> statements must be on the same line.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The <B>If\u2026Then\u2026 End If<\/B> statement must conclude with <B>End If<\/B>.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>End If<\/B> is two words, not one word.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>The VBScript <B>If\u2026Then\u2026End If<\/B> statement is seen in the <B>DemoIf.vbs<\/B> script.<\/P>\n<P><B>DemoIf.vbs<\/B><\/P><PRE class=\"codeSample\">a = 5\nIf a = 5 Then\n WScript.Echo &#8220;a equals 5&#8221;\nEnd If\n<\/PRE>\n<P>In the Windows PowerShell version of the <B>If\u2026Then\u2026End If<\/B> statement, there is neither a <B>Then<\/B> keyword nor an <B>End If<\/B> statement. From a typing perspective, the Windows PowerShell <B>If<\/B> statement is easier. This simplicity, however, comes with a bit of complexity. The condition that is evaluated in the <B>If<\/B> statement is positioned in a set of parentheses. In the <B>DemoIf.ps1<\/B> script, we are checking whether the variable <B>$a<\/B> is equal to 5. This is seen here:<\/P><PRE class=\"codeSample\">If ($a \u2013eq 5)\n<\/PRE>\n<P>The code that is executed when the condition is true is positioned inside a pair of braces (curly brackets). Code inside a pair of curly brackets is called a <I>script block<\/I> in Windows PowerShell, and script blocks are everywhere. The script block for the <B>DemoIf.ps1<\/B> script is seen here. <\/P><PRE class=\"codeSample\">{\n   &#8216;$a equals 5&#8217;\n}\n<\/PRE>\n<P>The Windows PowerShell version of the <B>DemoIf.vbs<\/B> script is the <B>DemoIf.ps1<\/B> script.<\/P>\n<P><B>DemoIf.ps1<\/B><\/P><PRE class=\"codeSample\">$a = 5\nIf($a -eq 5)\n {\n   &#8216;$a equals 5&#8217;\n }\n<\/PRE>\n<P>The hard part about how to work with the Windows PowerShell <B>If<\/B> statement is learning the comparison operators. In VBScript the equal sign (=) is used as an <B>assignment<\/B> operator. It is also used as an <B>equality<\/B> operator for comparison. On the first line of code, the variable <B>a<\/B> is assigned the value 5. This uses the equal sign as an assignment. On the next line of code, the <B>If<\/B> statement is used to see whether the value of a is equal to the number 5. On this line of code, the equal sign is used as the equality operator. This is seen here:<\/P><PRE class=\"codeSample\">a = 5\nIf a = 5 Then\n<\/PRE>\n<P>In simple examples such as this, it is fairly easy to tell the difference between an <B>equality<\/B> operator and an <B>assignment<\/B> operator. In more complex scripts, however, things could be confusing. Windows PowerShell removes that confusion by having special comparison operators. These are seen in Table 1. As mentioned earlier, this clarity comes with a bit of confusion, until you take the time to learn the PowerShell comparison operators. One thing that might help is to realize that the main operators are two letters long. The use of <B>-eq<\/B> stands for &#8220;equals,&#8221; and if you got that one right, it\u2019s not a stretch to understand that <B>-ne<\/B> stands for &#8220;not equal.&#8221; The one that used to trip me up was <B>\u2013ge<\/B> for &#8220;greater than or equal to.&#8221; For some reason, back when I was first learning Windows PowerShell, I would always type <B>\u2013gte<\/B> for &#8220;greater than or equal to.&#8221; Anyway, examine Table 1, print it out if you have to, and after several scripts the comparison operators will seem perfectly natural.<\/P>\n<TABLE id=\"ETAAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD>\n<TR>\n<TD class=\"tableHeader\" colSpan=\"4\">Table 1 Comparison Operators<\/TD><\/TR>\n<TR class=\"stdHeader\" vAlign=\"top\">\n<TD id=\"colEWAAC\">Operator<\/TD>\n<TD id=\"colE1AAC\">Description<\/TD>\n<TD id=\"colE5AAC\">Example<\/TD>\n<TD id=\"colECBAC\">Result<\/TD><\/TR><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-eq<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">equals<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = 5 ; $a -eq 4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">False<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-ne<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">not equal<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = 5 ; $a -ne 4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">True<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-gt<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">greater than<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = 5 ; $a -gt 4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">True<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-ge<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">greater than or equal to<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = 5 ; $a -ge 5<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">True<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-lt<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">less than<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = 5 ; $a -lt 5<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">False<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-le<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">less than or equal to<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = 5 ; $a -le 5<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">True<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-like<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">wildcard comparison<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = &#8220;This is Text&#8221; ; $a -like &#8220;Text&#8221;<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">False<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-notlike<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">wildcard comparison<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = &#8220;This is Text&#8221; ; $a -notlike &#8220;Text&#8221;<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">True<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-match<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">regular expression comparison<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = &#8220;Text is Text&#8221; ; $a -match &#8220;Text&#8221;<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">True<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">-notmatch<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">regular expression comparison<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">$a = &#8220;This is Text&#8221; ; $a -notmatch &#8220;Text$&#8221;<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">False<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now for the hard part DZ: &#8220;Why did the last thing that you typed work incorrectly?&#8221; When you typed <B>$a = 5<\/B>, you used an assignment operator:<\/P><PRE class=\"codeSample\">PS C:\\&gt; if ($a = 5) { &#8220;hi&#8221; }\nhi\n<\/PRE>\n<P>Remember that the <B>If<\/B> statement executes the script block if the condition evaluates to true. This can be illustrated perhaps more clearly by putting the <B>$true<\/B> variable into the condition as seen here:<\/P><PRE class=\"codeSample\">PS C:\\&gt; if ($true) { &#8220;its true&#8221; }\nits true\n<\/PRE>\n<P>If the condition evaluates to false, the script block does not execute. This is seen here:<\/P><PRE class=\"codeSample\">PS C:\\&gt; if ($false) { &#8220;its true&#8221; }\nPS C:\\&gt;\n<\/PRE>\n<P>Any assignment will evaluate to true, and therefore the script block is executed. In this example, we assign the value 1 to the variable <B>$a<\/B>. In the condition for the <B>If<\/B> statement, we assign the value of 12 to the variable <B>$a<\/B>. Any assignment evaluates to true, and the script block executes. <\/P><PRE class=\"codeSample\">PS C:\\&gt; $a = 1 ; If ($a = 12) { &#8220;its true&#8221; }\nits true\n<\/PRE>\n<P>Rarely do you test a condition and perform an outcome. Most of the time, you have to perform one action if the condition is true, and another action if the condition is false. In VBScript you used the <B>If\u2026Else\u2026End If<\/B> construction. The <B>Else<\/B> clause went immediately after the first outcome to be performed if the condition were true. This is seen in the <B>DemoIfElse.vbs<\/B> script:<\/P>\n<P><B>DemoIfElse.vbs<\/B><\/P><PRE class=\"codeSample\">a = 4\nIf a = 5 Then\n WScript.Echo &#8220;a equals 5&#8221;\nElse\n WScript.Echo &#8220;a is not equal to 5&#8221;\nEnd If\n<\/PRE>\n<P>In Windows PowerShell the syntax is not surprising. Following the closing curly brackets from the <B>If<\/B> statement script block, you add the <B>Else<\/B> keyword and open a new script block to hold the alternative outcome. This is seen here:<\/P>\n<P><B>DemoIfElse.ps1<\/B><\/P><PRE class=\"codeSample\">$a = 4\nIf ($a -eq 5)\n{\n &#8216;$a equals 5&#8217;\n}\nElse\n{\n &#8216;$a is not equal to 5&#8217;\n}\n<\/PRE>\n<P>Things become confusing with VBScript when you want to evaluate multiple conditions and have multiple outcomes. The <B>Else If<\/B> clause provides for the second outcome. You have to evaluate the second condition. The <B>Else If<\/B> clause receives its own condition, which is followed by the <B>Then<\/B> keyword. Following the <B>Then<\/B> keyword, list the code that you want to execute. This is followed by the <B>Else<\/B> keyword and a pair of <B>End If<\/B> statements. This is seen in the <B>DemoIfElseIfElse.vbs<\/B> script:<\/P>\n<P><B>DemoIfElseIfElse.vbs<\/B><\/P><PRE class=\"codeSample\">a = 4\nIf a = 5 Then\n WScript.Echo &#8220;a equals 5&#8221;\nElse If a = 3 Then\n WScript.Echo &#8220;a equals 3&#8221;\nElse\n WScript.Echo &#8220;a does not equal 3 or 5&#8221;\nEnd If\nEnd If\n<\/PRE>\n<P>The Windows PowerShell <B>DemoIfElseIfElse.ps1<\/B> script is a bit easier to understand because it avoids the double <B>End If<\/B> kind of scenario. For each condition that you want to evaluate, you use <B>ElseIf<\/B> (be aware that it is a single word). You put the condition inside a pair of parentheses, and open your script block. Here is the <B>DemoIfElseIfElse.ps1<\/B> script:<\/P>\n<P><B>DemoIfElseIfElse.ps1<\/B><\/P><PRE class=\"codeSample\">$a = 4\nIf ($a -eq 5)\n{\n &#8216;$a equals 5&#8217;\n}\nElseIf ($a -eq 3)\n{\n &#8216;$a is equal to 3&#8217;\n}\nElse\n{\n &#8216;$a does not equal 3 or 5&#8217;\n}\n<\/PRE>\n<P>DZ, as a best practice, I generally avoid using the <B>ElseIf<\/B> type of construction from either VBScript or Windows PowerShell because there is a much better way to write the same code. We will examine that construction tomorrow. I hope that you enjoyed today&#8217;s excursion into the <B>If<\/B> statement. Join us tomorrow. Until then, keep on scripting.<\/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 have a problem with a script I am trying to write. I have to evaluate between several conditions as they come into the script, but I do not understand the way the If statement in Windows PowerShell works. I even tried to use the Get-Help cmdlet to find Help, but when [&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,45],"class_list":["post-53813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a problem with a script I am trying to write. I have to evaluate between several conditions as they come into the script, but I do not understand the way the If statement in Windows PowerShell works. I even tried to use the Get-Help cmdlet to find Help, but when [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53813","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=53813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53813\/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=53813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}