In this post let’s understand conditional logic in Powershell with various examples. Earlier I have covered 2 posts about the Basics of Powershell and Loops in Powershell. If you are not clear with this post I strongly recommend you to check out the other post in the Blog section.
What are the conditional logic in Powershell?
We all know that Powershell consists of some extremely useful commands and as well as useful decision-making logic. It doesn’t differ from other coding or scripting languages. In Powershell, we use If-else and switch case to do a task that is decision making related. I am truly excited to discuss these 2 statements with examples to have a better understanding of them.
How to use the If-Else statement in Powershell?
Let’s understand a situation where we will use the If-else statement. When you want to execute certain statements which are meant to be executed when the condition is true or false. Below is the syntax we use for If-Else statement.
if () {statement }
else {statement}
“If” is always provided with conditions ad later the statement and else is provided with the statement. When a condition fails to meet the requirement the “Else” statement will be executed. Let’s understand this with examples.
Example 1:
$a = "Hi Powershellguru"
$b = "Hi guru"
if ($a -match $b)
{write-host "both are same"}
else{ Write-Host "both are different"}
Output: both are different
Example 2:
$a = "Hi Powershellguru"
$b = "Hi Powershellguru"
if ($a -match $b)
{write-host "both are same"}
else{ Write-Host "both are different"}
Output: both are same
How to use Switch Statement in Powershell?
Ever used multiple If statements in a script but you will be shocked to know that we just could have used a switch case instead which can manage multiple statements in it. Let’s see the syntax for the switch case and discuss further regarding Switch.
Switch (Value)
{
Condition1 {action1}
Condition2 {action2}
Condition3 {action3}
default {statement}
}
The switch case is self-explanatory if in case any one of the statement doesn’t meet the required criteria the default statement is executed. Let’s see some of the examples we can test for Switch case.
Example 1:
switch (3)
{
1 {"It is one."}
2 {"It is two."}
3 {"It is three."}
4 {"It is four."}
}
Output: It is three.
Example 2:
switch (3)
{
1 {"It is one."}
2 {"It is two."}
3 {"It is three."}
4 {"It is four."}
3 {"Three again."}
}
Output: It is three.
Three again.
All you need to know about Comparison Operators
Comparison operators are used widely in Powershell when it comes to comparing anything. Not only Powershell it is used in every language. For Powershell below is the operator that is being used.
Source: Microsoft
Type | Operator | Comparison test |
---|---|---|
Equality | -eq | equals |
 | -ne | not equals |
 | -gt | greater than |
 | -ge | greater than or equal |
 | -lt | less than |
 | -le | less than or equal |
Matching | -like | string matches wildcard pattern |
 | -notlike | string does not match wildcard pattern |
 | -match | string matches regex pattern |
 | -notmatch | string does not match regex pattern |
Replacement | -replace | replaces strings matching a regex pattern |
Containment | -contains | collection contains a value |
 | -notcontains | collection does not contain a value |
 | -in | value is in a collection |
 | -notin | value is not in a collection |
Type | -is | both objects are the same type |
 | -isnot | the objects are not the same type |
Below are some examples which you can test in Powershell and can use the above operators in the same way.
2 -eq 2 # Output: True
2 -eq 3 # Output: False
"abc" -eq "abc" # Output: True
"abc" -eq "abc", "def" # Output: False
"abc" -ne "def" # Output: True
"abc" -ne "abc" # Output: False
"abc" -ne "abc", "def" # Output: True
Conclusion
I hope you understood the comparison operators and conditional logic. As we are moving forward towards the end I want you to practice if you are a newbie to Powershell. I guarantee you if you follow all my posts you will excel in Powershell.