If Else 3.jpg

How To Use If Else Statements in PowerShell

If Else statements let you add conditional logic statements to your PowerShell scripts. Read on for instructions and examples.

PowerShell offers several tools for performing conditional logic, such as if-then-else statements. However, these statements are called If Else statements in PowerShell because PowerShell does not use the word “then.”

‘If’ Statements Syntax in PowerShell

Before delving into If Else statements, let’s first look at how If statements work in PowerShell.

If statements tend to be really simple. They are made up of three parts: the word “if,” a condition, and an action that is performed if the condition is met. The condition is enclosed in parenthesis, and the action is enclosed in brackets.

Example of an If Statement

For the sake of demonstration, here is an example of an If statement.

I am going to create a variable called $A and set its value to 5. I will then create an If statement that checks to see if $A is equal to 5. If $A is indeed equal to 5, I will have PowerShell display the words “It’s a match.”

Here is the code:

$A=5

If ($A -eq 5) {Write-Host ‘Its a match’}

The first line of code creates a variable called $A and sets its value to 5. The second line of code is the If statement. It starts with the word “if,” followed by a condition in parenthesis. In this case, the code is checking to see if the variable is equal to 5 ($A -eq 5). Assuming $A is equal to 5, which it should be, PowerShell performs the specified action and displays text saying, “It’s a match.”

You can see what this looks like in Figure 1. (Note: The apostrophe in “it’s a match” is omitted because it would break the script.)

Brien PoseyScreenshot shows example of an If statement in PowerShell

Figure 1. This is what an If statement looks like in PowerShell.

So, what would happen if the $A variable were set to something other than 5? In Figure 2, you can see that I set $A equal to 6 and then performed the same If statement as before. This time nothing happens because the condition ($A -eq 5) is not being satisfied, which means that the action (displaying “its a match”) is never performed.

Brien Posey When $A is set to a value of 6, no action is performed.

Figure 2. When $A is set to a value of 6, no action is performed.

‘If Else’ Statement Syntax in PowerShell

This is where If Else statements come into play.

An If Else statement causes PowerShell to perform one action if the specified condition is satisfied and perform a different action if the condition is not satisfied.

The first half of an If Else statement (i.e., the If statement) is identical to what was already demonstrated above. It starts with the word “If” and is followed by a condition that appears inside parenthesis and an action that appears between brackets.

The Else portion of the command is appended to the If command, just after the action. The Else portion of the command does not need a condition (because a condition is already being evaluated by the If statement), but it does need an action. As with the If statement, the action is enclosed in brackets.

Example of an If Else Statement

Let’s look at an If Else example. We’ll create an If Else statement that checks to see if $A is equal to 5. If $A does equal 5, then PowerShell will display the word “Match.” If $A is equal to something else, then PowerShell will display the words “No Match.”

Here is what the code looks like:

If ($A -eq 5) {Write-Host ‘Match’} Else {Write-Host ‘No Match’}

See how this code works in Figure 3.

If Else 3.jpg

Figure 3. This is how an If Else statement works in PowerShell.

If Else Statements Can Contain Multiple Conditions

Although I have kept things simple, note that real-world use cases of If Else statements in PowerShell can be more complex.

An If statement, for example, might include multiple conditions, such as the following:

If ($A -eq 5 -AND $B -eq 2)

In this case, I am checking the values of two variables. Because I used -And (as opposed to -Or), both conditions must be true for the action to be performed.

Best Practice: Place Multiple Lines of Code in a Function

It is also worth noting that actions can include multiple lines of code. As a best practice, however, if an action requires multiple lines of code, consider placing that code in a function and call the function from the If Else statement.

About the author

Brien Posey headshotBrien Posey is a bestselling technology author, speaker, and 21x Microsoft MVP. In addition to his ongoing work in IT, Posey has trained as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.
TAGS: How To...?
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish