if else and else if
suggest changePowerShell supports standard conditional logic, much like many programming languages. It lets you run commands only in particular circumstances.
With an if, the commands inside the braces ({}) run only if the condition in parentheses is true:
$test = "test"
if ($test -eq "test"){
Write-Host "if condition met"
}
You can also do an else. Here the else commands are executed if the if conditions are not met:
$test = "test"
if ($test -eq "test2"){
Write-Host "if condition met"
}
else{
Write-Host "if condition not met"
}
or an elseif. An else if runs the commands if the if conditions are not met and the elseif conditions are met:
$test = "test"
if ($test -eq "test2"){
Write-Host "if condition met"
}
elseif ($test -eq "test"){
Write-Host "ifelse condition met"
}
The examples use -eq (the equality operator), not = or ==. = is assignment. == is not a PowerShell comparison operator.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents