Hey, Scripting Guy! How Can I Use The If Statement in Windows PowerShell - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs
Hey, Scripting Guy! How Can I Use The If Statement in Windows PowerShell - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs
Hey,ScriptingGuy!HowCanIUsetheIfStatementinWindowsPowerShell?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs
All About
Windows Server
Cloud Platform
Blogs
Datacenter
Management
Client
Management
Virtualization,
VDI & Remote
Desktop
Windows Server
Management
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 GetHelp cmdlet to find Help, but when I use GetHelp If, it says it cannot find Help for that topic. Surely
Windows PowerShell contains an If 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. Im attaching a picture. Can you help me figure it out?
DZ
Hi DZ,
I will give you credit for persistence! In Windows PowerShell if you want to use GetHelp to learn about something, look
for an "about topic". Interestingly, if you were using Windows PowerShell 2.0 currently in CTP 3, your first query would
have worked and you would have found the about_if topic. Here is the Help query that you should have used:
GetHelpabout_if
In Windows PowerShell 2.0, the GetHelp cmdlet uses a regular expression match to find Help topics. As seen here, both
cmdlet Help and about topic Help are searched.
PSC:\>GetHelpif
NameCategorySynopsis
NewModuleManifestCmdletCreatesanewmodulemanifest.
TestModuleManifestCmdletVerifiesthatamodulemanifest...
GetPfxCertificateCmdletGetsinformationabout.pfxcert...
about_ifHelpFileDescribesalanguagecommandyou...
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 Windows Vista,
Windows XP, and Windows Server 2003. The Windows PowerShell Scripting Hub is a good place to start using Windows
PowerShell. An excellent book for learning Windows PowerShell is the Microsoft Press book, Microsoft Windows
PowerShell Step by Step. This book has many handson labs and uses realworld examples to show the use of Windows
PowerShell.
http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/06/howcaniusetheifstatementinwindowspowershell.aspx
1/5
1/15/2015
Hey,ScriptingGuy!HowCanIUsetheIfStatementinWindowsPowerShell?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs
In VBScript the IfThenEnd If statement was somewhat straightforward. There were several things to be aware of:
Example
Result
eq
equals
$a = 5 ; $a eq 4
False
ne
not equal
$a = 5 ; $a ne 4
True
gt
greater than
$a = 5 ; $a gt 4
True
ge
$a = 5 ; $a ge 5
True
lt
less than
$a = 5 ; $a lt 5
False
le
$a = 5 ; $a le 5
True
http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/06/howcaniusetheifstatementinwindowspowershell.aspx
2/5
1/15/2015
Hey,ScriptingGuy!HowCanIUsetheIfStatementinWindowsPowerShell?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs
like
wildcard comparison
False
notlike
wildcard comparison
True
match
True
Now for the hard part DZ: "Why did the last thing that you typed work incorrectly?" When you typed $a = 5, you used an
assignment operator:
PSC:\>if($a=5){"hi"}
hi
Remember that the If statement executes the script block if the condition evaluates to true. This can be illustrated perhaps
more clearly by putting the $true variable into the condition as seen here:
PSC:\>if($true){"itstrue"}
itstrue
If the condition evaluates to false, the script block does not execute. This is seen here:
PSC:\>if($false){"itstrue"}
PSC:\>
Any assignment will evaluate to true, and therefore the script block is executed. In this example, we assign the value 1 to
the variable $a. In the condition for the If statement, we assign the value of 12 to the variable $a. Any assignment
evaluates to true, and the script block executes.
PSC:\>$a=1;If($a=12){"itstrue"}
itstrue
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 IfElseEnd If construction. The Else clause
went immediately after the first outcome to be performed if the condition were true. This is seen in the DemoIfElse.vbs
script:
DemoIfElse.vbs
a=4
Ifa=5Then
WScript.Echo"aequals5"
Else
WScript.Echo"aisnotequalto5"
EndIf
In Windows PowerShell the syntax is not surprising. Following the closing curly brackets from the If statement script block,
you add the Else keyword and open a new script block to hold the alternative outcome. This is seen here:
DemoIfElse.ps1
$a=4
If($aeq5)
{
'$aequals5'
}
Else
{
'$aisnotequalto5'
}
Things become confusing with VBScript when you want to evaluate multiple conditions and have multiple outcomes. The
Else If clause provides for the second outcome. You have to evaluate the second condition. The Else If clause receives its
own condition, which is followed by the Then keyword. Following the Then keyword, list the code that you want to
execute. This is followed by the Else keyword and a pair of End If statements. This is seen in the DemoIfElseIfElse.vbs
script:
DemoIfElseIfElse.vbs
a=4
Ifa=5Then
WScript.Echo"aequals5"
ElseIfa=3Then
WScript.Echo"aequals3"
Else
WScript.Echo"adoesnotequal3or5"
EndIf
EndIf
http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/06/howcaniusetheifstatementinwindowspowershell.aspx
3/5
1/15/2015
Hey,ScriptingGuy!HowCanIUsetheIfStatementinWindowsPowerShell?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs
The Windows PowerShell DemoIfElseIfElse.ps1 script is a bit easier to understand because it avoids the double End If
kind of scenario. For each condition that you want to evaluate, you use ElseIf 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 DemoIfElseIfElse.ps1 script:
DemoIfElseIfElse.ps1
$a=4
If($aeq5)
{
'$aequals5'
}
ElseIf($aeq3)
{
'$aisequalto3'
}
Else
{
'$adoesnotequal3or5'
}
DZ, as a best practice, I generally avoid using the ElseIf 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's excursion into the If statement. Join us tomorrow. Until then, keep on scripting.
Tweet
Share
Comments
17 Jun 2013 1:50 PM
Suparno
BrianJester
Thank you
21 Aug 2013 10:49 PM
Ed Wilson
I am trying to check connection for IP address and delete DHCP lease if the connection is false. So far this is what
I have.
if TestConnection ComputerName 10.5.5.100 Count 1 Quiet eq False {netsh dhcp server \\computername
scope 10.5.5.0 delete lease 10.5.5.100
I know both commands work but I cant get the syntax right
Victor Ashiedu
good stuff
http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/06/howcaniusetheifstatementinwindowspowershell.aspx
4/5
1/15/2015
Hey,ScriptingGuy!HowCanIUsetheIfStatementinWindowsPowerShell?Hey,ScriptingGuy!BlogSiteHomeTechNetBlogs
17 Dec 2014 4:56 PM
JPJ
http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/06/howcaniusetheifstatementinwindowspowershell.aspx
5/5