MVP Systems Blog

Wednesday, September 29, 2010
« Another Convert |
Main |
PowerShell Training »
Mastering PowerShell scripting often involves comparing things. This includes learning how to branch using if/then/else, how to build loops with while and foreach, etc. For example, consider this code:
if ($x –eq 1) {
write-host "x is 1"
} else {
write-host "no it isn't"
}
So how, exactly, do if and else work? Help is available for all these things, but in some cases it’s hard to track down. PowerShell’s help contains a lot of topics in the form of about_something. For example, there is an about_if, which talks about both the if and else statements. You can see all of them by typing “help *about*”. The other thing to note in the statement above is the use of “-eq”. This is a so-called comparison operator, and mastering these is very important. Type “help about_comparison_operators” to learn all about these.
Here are some common comparison operators:
Operator
-eq Equal To
-gt Greater Than
-match Matches a string using regular expressions
-ne Not Equal To
-contains Containment operator includes
-not Containment operator does NOT includes
-lt Less Than