O'Reilly logo

Monad (AKA PowerShell) by Andy Oakley

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

Control Script Flow with Comparisons

Now that we have all this state to work with, it's time to start making some decisions. We have already touched on how to make tests and comparisons in Chapter 1, and we'll be using the same operators here. In this section, we'll explore the if and switch statements and see how they can be used, depending on certain conditions, to execute different parts of a script.

How Do I Do That?

Starting with some simple cases, let's make comparisons between two constants. This will lay the foundation for what is to come:

    MSH D:\MshScripts> 7 -gt 5
    True

    MSH D:\MshScripts> 7 -eq 5
    False

    MSH D:\MshScripts> "alpha" -lt "zulu"
    True

No surprises here. MSH is performing the comparison and putting a Boolean (true/false) value into the pipeline based on the outcome. Tests become more interesting when we compare a dynamic value against a constant one, but, in every case, a test will always generate a true/false value. ...

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