Conditional control – if, else, elseif, and switch
Conditional control flow is based on a simple statement. If this thing is true, then do that thing. The first part is a Boolean expression. The second part is an action based on the resolution of the Boolean. A Boolean expression will resolve to one of two values: true or false. We discussed Boolean operators in Chapter 3, The PowerShell Pipeline – How to String Cmdlets Together, in the Understanding Where-Object advanced syntax section. Consider the following two statements:
1 -eq 1 1 -eq 2
The first one resolves to the Boolean-type object true. The second one resolves to false. Try it if you don’t believe me. It’s not often that we need to check whether 1 is the same as 2, but we frequently need to compare variables:
Figure 5.6 – Just checking
That covers the first part of our logic; next, we need to be able to perform an action based on the result of our test...