You can use seven different comparison
operators in Windows PowerShell, and each starts with a hyphen (-) followed by a twoletter
abbreviation of the comparison it performs:
?–? -eq Equal to
?– -ne Not equal to
?– -notmatch Does not match
?– -gt Greater than
?– -ge Greater than or equal to
?– -lt Less than
?–? -le Less than or equal to
Another method for performing a conditional branching within your code is through
the use of a Switch statement. A Switch statement is a more efficient way of handling
situations in which you want to test more than two conditions with an If/Elseif statement.
For example, let??™s say you have a variable that can contain the name of one of
seven different colors??”Red, Blue, Yellow, White, Green, Orange, Black??”and you want
to perform different actions based on each individual color. If you could only use If/
Elseif statements, it would take many such statements and would not be easy to read
later. Using a Switch statement makes the code much neater and intuitive, as in this
example:
$color = "blue"
switch ($color) {
red {write-host "Color Red"; break}
blue {write-host "Color Blue"; break}
yellow {write-host "Color Yellow"; break}
white {write-host "Color White"; break}
green {write-host "Color Green"; break}
orange {write-host "Color Orange"; break}
black {write-host "Color Black"; break}
}
Notice how easy it is to see which code gets executed based on the value of $color.
Pages:
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476