Conditional statements are used to execute specific commands only if a certain condition is met. In PowerShell we use the key words “if”, “elseif”, and “else” to guide the script through the different potential conditions.
These statements allow us to create more sophisticated scripts with code that is more flexible to different use-cases. By implementing conditional statements, we are able to save time as our code single segment of code will be able to make the appropriate decision based on the situation.
4th PowerShell Script
The forth PowerShell script created is:
These 3 statements demonstrate the ability to apply conditions to simple and complex requests. It is possible to use conditional statements that take multiple forms of input and can provide a relatively short piece of code that will run multiple checks against an input.
Statement 1
The first statement provides a simple method of outputting a string if a condition is met. This is achieved with the if command. (In PowerShell instead of using > symbol we use -gt, this stands for “greater than”)
Statement 2
The second statement uses “elif” and “else” to expand on statement 1. When comparing two integers, there are three potential outcomes:
· x is bigger than y
· x is smaller than y
· x is the same as y
elseif is the keyord we use to say that as the condition of the previous if statement has not been met, another if statement is provided. Once all of the elseif statements have been checked, if the
condition still has not been met, the “else” keyword is used as a catch-all statement to mean “if none of the above if statements are correct, then do this”
Statement 3
The third statement shows that we are able to use conditional statements as part of a function. As with all functions, we are able to pass parameter values. It is therefore possible to create functions with varying levels of complexity that, in this example, can show the ability to take input and work through the script until a condition is met.
The examples we have here show that we can pass both string and integer values and determine whether an exam has been passed or not. It is important to note that when creating conditional statements, it is important to create an if or elseif action for each valid condition and to use else to catch any outlier.
The forth PowerShell script created is:
#conditional statements $x=5 $5=10 if ($y -gt $x) { Write-Output "$y is bigger than $x" } #conditional statement with an elseif $x=50 $y=50 if ($y -gt $x) { Write-Output "y is bigger than x" } elseif ($y -lt $x) { Write-Output "x is bigger than y" } else { Write-Output "both x and y are the same" } #conditional statement within a function function exam($practical,$quiz){ if ($practical -eq "pass" -and $quiz -lt 60) { Write-Output "you have passed the practical but failed the quiz" } elseif ($practical -eq "pass" -and $quiz -ge 60) { Write-Output "you have passed both parts of the exam" } elseif ($practical -eq "fail" -and $quiz -ge 60) { Write-Output "you have failed the practical but passed the quiz" } else { Write-Output "you have failed both parts of the exam"
} } exam "pass" 80 exam "fail" 20