Chapter 4. Looping and Flow Control
Introduction
As you begin to write scripts or commands that interact with unknown data, the concepts of looping and flow control become increasingly important.
PowerShell’s looping statements and commands let you perform an operation (or set of operations) without having to repeat the commands themselves. This includes, for example, doing something a specified number of times, processing each item in a collection, or working until a certain condition comes to pass.
PowerShell’s flow control and comparison statements let you adapt your script or command to unknown data. They let you execute commands based on the value of that data, skip commands based on the value of that data, and more.
Together, looping and flow control statements add significant versatility to your PowerShell toolbox.
Make Decisions with Comparison and Logical Operators
Problem
You want to compare some data with other data and make a decision based on that comparison.
Solution
Use PowerShell’s logical operators to compare pieces of data and make decisions based on them.
- Comparison operators
-eq,-ne,-ge,-gt,-lt,-le,-like,-notlike,-match,-notmatch,-contains,-notcontains,-is,-isnot- Logical operators
-and,-or,-xor,-not
For a detailed description (and examples) of these operators, see Comparison Operators.
Discussion
PowerShell’s logical and comparison operators let you compare pieces of data or test data for some condition. An operator either compares two pieces of data (a binary ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access