O'Reilly logo

Learn PowerShell Toolmaking in a Month of Lunches by Don Jones and Jeffrey Hicks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.7. Logical constructs

Logical constructs are used to make decisions and to execute different commands based on the outcome of that decision.

3.7.1. If construct

This is PowerShell’s main decision-making construct. In its full form, it looks like this:

If ($this -eq $that) {
  # commands
} elseif ($those -ne $them) {
  # commands
} elseif ($we -gt $they) {
  # commands
} else {
  # commands
}

The If keyword is the only mandatory part of this construct. Following it is a parenthetical expression that must evaluate to either True or False—although PowerShell will always interpret 0 (zero) as False and any nonzero value as True. PowerShell also recognizes the built-in variables $True and $False as representing those Boolean values. If the expression ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required