PowerShell provides a comprehensive set of flow-control constructs, starting with if/elseif/else as follows:
$Value = 2
If ($Value -eq 1) {
Do-Something
}
ElseIf ($Value -eq 2) {
Do-SomethingElse
}
Else {
Do-SomethingCompletelyDifferent
}
Note that we have a completely different set of comparison and logical operators to remember as follows:
Equals | -eq |
Not Equal To | -ne |
Greater Than | -gt |
Greater Than or Equal To | -ge |
Less Than | -lt |
Less Than or Equal To | -le |
Not | -not or ! |
And | -and |
Or | -or |
Additionally, if you're doing string comparison, you can force case-sensitive or case-insensitive (default comparison is case-insensitive) by prefacing the operator with a c or i e.g. -ceq is a case-sensitive equal comparison.
In addition to the if flow control, PowerShell also has:
- Do While - Do {Code Block} While (Condition)
- While - While (Condition) {Code Block}
- Do Until -
- For - For ($Index = 1; $Index -le 3; $Index++) {Code Block}
- ForEach - ForEach ($MyObject In $MyObjectsArrayOrCollection) {Code Block}
- Switch -
Switch ($MyValue)
{
Result1 {Code Block}
Result2 {Code Block}
Default {Code Block}
}
A couple of good articles on flow control are:
No comments:
Post a Comment