Chapter 6. A Bit of Logic to Save the Day

In This Chapter

  • Understanding logical operators

  • Using conditional statements to affect script flow

  • Creating loops to repeat tasks

  • Avoiding common loop pitfalls

Computers are simply oversized and overpowered calculators. Sometimes it's hard to explain this to the average nontechie, but everything we do on our computers — sending and receiving e-mail, browsing the Web, watching movies — all comes back to numbers! In fact, everything comes down to two numbers: 0 and 1 (nothing and something).

Take away all the fancy processor architecture, bus speeds, and cool case lights on a computer, and you have a machine that's good at adding ones and zeroes and also at comparing true and false. Everything else that you see, hear, and interact with on your computer is some sort of representation of these fundamental units.

In this chapter, you give your Windows PowerShell scripts a little more intelligence by building on these concepts of true and false to direct the flow of information through your scripts.

A Logic Primer

Logic is built around the premise of true and false values, which are known as Boolean values. In general, the digit 1 represents true, and the digit 0 represents false. Windows PowerShell, however, also has automatic variables that define the values of true and false — namely, $TRUE and $FALSE.

When working with Boolean values, you can use a set of operations to combine and compare them. These operations are

  • AND: When you compare two values by ...

Get Windows PowerShell™ 2 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.