The If statement can be used to add some conditional logic to you script.
The conditional logic can be extended with elseif and else.
A single If statement:
if (condition) {
# your code here
}
If - else syntax:
if (condition) {
# your code here
} else (condition) {
# your code here
}
All together:
if (condition) {
# your code here
} elseif (condition) {
# your code here
} else (condition) {
# your code here
}
The condition that you check must return a boolean (True or False).
Elseif is used when the the previous condition was False.
You can use Else when previous If or Elseif conditions are False.
Sometimes it's better if you use a Switch statement rather than an If-Elseif-Else structure.
Testing our color:
$testValue = "red"
if ($testValue -eq green) {
Write-Host "oh yeah funky green"
} elseif ($testValue -eq blue) {
Write-Host "I'm blue dabadee dabadaa"
} elseif ($testValue -eq pink) {
Write-Host "Hi, i'm pinky"
} elseif ($testValue -gt red) {
Write-Host "Yes, it's me, reddy!"
} else {
"Not a fancy color"
}