Running the script, we can see that since $age is set to 75, the third condition is met, and so the third action executes. Let’s change the value of $age and see how the other actions are executed based on their conditions: Running the script, we can see that since $age is set to 12, the first condition is met, and so the first action executes. Running the script, we can see that since $age is set to 34, the second condition is met, and so the second action executes. – The third Condition says that the $age must be greater than or equal to (-ge) 65.įinally, we specify what the Action will be for each condition if met. – The second Condition says that the $age must be greater than or equal to (-ge) 18, AND (-and) less than (-lt) 65. – The first Condition says that the $age must be less than (-lt) 18. We can use this if we want different actions to take place based on different conditions. We can also use multiple If statements, each with its own condition. Let’s change the value of $testScore to something else and run the script again: We can see that since the value of $testScore is not greater than 85, the Action did not execute. The Action is a message that reads “The student PASSED the test.” Running the script, we see that the condition is met, and the action is displayed. The Condition says that $testScore must be greater than (-gt) 85.įinally, we specify what the Action will be if the Condition is met. We then declare the If condition inside the parentheses. If the condition is not met, the action is not executed.įirst we are declaring a variable $testScore and giving it a value of 93. If the condition is met, the action executes. The condition must be met for the action to execute. Notice how the condition is set between parentheses (), and the action is set between curly braces. The If statement composition is the following: If gender equals to male, then name the baby Kevin. For example, if age is over 65, then classify the person as Senior. The If statement is used when we want something to be done based on a condition. In this tutorial we will go over the If statement in PowerShell.