Prof. Powershell

Lessons in Logic

How to use an IF construct in a PowerShell script.

If you've done any sort of scripting, you've likely used logic constructs like IF, THEN and ELSE. PowerShell can also use these logic constructs, and I'll introduce some of them to you here. But don't think you need a script to use them-you can use these constructs directly in the console. Let's look at an IF construct. In PowerShell, this is the minimum syntax:

PS C:\> if (5 -gt 2) {write-host "bigger"}
bigger

Notice that there's no THEN and no END IF. The IF keyword evaluates the expression in parentheses and, if TRUE, executes whatever commands are enclosed in the curly braces. In this example, the word "bigger" is written to the console. Also, in this example, if the expression evaluated FALSE, you wouldn't get anything.

Here's where you can add an ELSE clause:

PS C:\> $x=5
PS C:\> if ($x -gt 2) {write-host "X is bigger"} else {write-host "X 
is smaller"}
X is bigger

If the expression is TRUE, then the first script block is executed; otherwise, the second script block is executed. You can also perform secondary comparisons using an ELSEIF clause:

PS C:\> $x=5
PS C:\> if ($x -gt 10) {write-host "X is bigger"} elseif ($x -ge 5) 
{write-host "X is in the middle"} else {write-host "X is smaller"}
X is in the middle

In this example, I'm evaluating three conditions:

  • If $x is greater than 10, then the first script block is executed.
  • Otherwise (ELSEIF), if $x is greater or equal than 5, then the second script block is executed.
  • If neither is TRUE, the last script block is executed.

Remember that PowerShell will only execute the script block for the first expression that evaluates TRUE.

PowerShell doesn't care about the formatting and spacing of the braces, so you can write it as one line in the console (though in a script you'll likely write it like this):

if ($x -gt 10) 
{
  write-host "X is bigger"
}
  elseif ($x -ge 5) 
{
  write-host "X is in the middle"
} 
else 
{
  write-host "X is smaller"
}

This format makes it easier to understand, especially if you have multiple ELSEIF clauses.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Tue, Dec 16, 2008 Anonymous Anonymous

ceguyomvpaxcetgolubxagbjzlwlcr

Tue, Dec 16, 2008 Anonymous Anonymous

ceguyomvpaxcetgolubxagbjzlwlcr

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.