ABOUT_IF Short description Describes a language command you can use to run statement lists based on the results of one or more conditional tests. Long description You can use the if statement to run code blocks if a specified conditional test evaluates to true. You can also specify one or more additional conditional tests to run if all the prior tests evaluate to false. Finally, you can specify an additional code block that's run if no other prior conditional test evaluates to true. Syntax The following example shows the if statement syntax: if () {} [elseif () {}] [else {}] When you run an if statement, PowerShell evaluates the conditional expression as true or false. If is true, runs, and PowerShell exits the if statement. If is false, PowerShell evaluates the condition specified by the conditional statement. For more information about boolean evaluation, see about_Booleans. If is true, runs, and PowerShell exits the if statement. If both and evaluate to false, the code block runs, and PowerShell exits the if statement. You can use multiple elseif statements to chain a series of conditional tests. Each test is run only if all the previous tests are false. If you need to create an if statement that contains many elseif statements, consider using a Switch statement instead. Examples: The simplest if statement contains a single command and doesn't contain any elseif statements or any else statements. The following example shows the simplest form of the if statement: if ($a -gt 2) { Write-Host "The value $a is greater than 2." } In this example, if the $a variable is greater than 2, the condition evaluates to true, and the statement list runs. However, if $a is less than or equal to 2 or isn't an existing variable, the if statement doesn't display a message. By adding an Else statement, a message is displayed when $a is less than or equal to 2. As the next example shows: if ($a -gt 2) { Write-Host "The value $a is greater than 2." } else { Write-Host ("The value $a is less than or equal to 2," + " is not created or is not initialized.") } To further refine this example, you can use the elseif statement to display a message when the value of $a is equal to 2. As the next example shows: if ($a -gt 2) { Write-Host "The value $a is greater than 2." } elseif ($a -eq 2) { Write-Host "The value $a is equal to 2." } else { Write-Host ("The value $a is less than 2 or" + " was not created or initialized.") } See also - about_Booleans - about_Comparison_Operators - about_Switch