PSMonday #30: November 21, 2016

Topic: If, If-Else, If-ElseIf III

Notice: This post is a part of the PowerShell Monday series — a group of quick and easy to read mini lessons that briefly cover beginning and intermediate PowerShell topics. As a PowerShell enthusiast, this seemed like a beneficial way to ensure those around me at work were consistently learning new things about Windows PowerShell. At some point, I decided I would share these posts here, as well. Here’s the PowerShell Monday Table of Contents.

I failed to mention something last week. While I wouldn’t recommend you do it, you can rewrite this:

If (-Not(<condition>)) {
    <action>
}

to be this:

If (!(<condition>)) {
    <action>
}

I’m not mentioning this because you should use ! over -Not (ever, honestly), but just in case you see it being used. As I believed we’ve mentioned before, we only use aliases in onetime use scenarios, such as when you’re entering commands in your ConsoleHost or the ISE and have no intention of saving what you’ve entered. The ! is not a true alias, but it’s close enough to consider it one. This is a PowerShell community best practice, and one I’ll continue to pound in, at every opportunity I have.

Okay, on with this week’s If wrap-up.

Let’s get away from these conceptual examples. In this first example, we’ll assign values to two variables: $y will hold the numeric value of 5, and $z will hold the numeric value of 3. When we enter the If statement, we check both values. If both — and that’s key — of their values match, we’ll echo the word “If.”

$y = 5; $z = 3

If ($y -eq 5 -and $z -eq 3) {
    Write-Output -InputObject 'If'
}

If

Had we used the -or comparison operator, it would’ve echoed “If,” as well. Or, means that only one of the conditions would have to result In True.

If ($y -eq 5 -or $z -eq 10) {
    Write-Output -InputObject 'If'
}

If

In the above example, only $y -eq 5 was True. As mentioned, since we used the -or operator, only one of those conditions needed to result in True for the condition to pass. The below example uses the -and comparison operator again; however, the end results is False, so the Else portion of the If-Else is executed.

$y = 5; $z = 3

If ($y -eq 5 -and $z -eq 2) {
    Write-Output -InputObject 'If'
} Else {
    Write-Output -InputObject 'Else'
}

Else

Pretty straightforward. Let’s modify the above examples and make our Else, and ElseIf. When this example executes, the If portion will be False ($z was not assigned a numeric value of 2), so the ElseIf condition will be checked. This will result in True, as $z was assigned a numeric value of 3.

$y = 5; $z = 3

If ($y -eq 5 -and $z -eq 2) {
    Write-Output -InputObject 'If'
} ElseIf ($y -eq 5 -and $z -eq 3) {
    Write-Output -InputObject 'ElseIf'
}

ElseIf

In our final example we’ll include two ElseIf conditions. Notice in this example, that once the first one results in True, we exit the construct. That final ElseIf, while it would have also resulted in True, isn’t even considered. While we won’t go into it for now, you can nest language constructs. This is to say, we could’ve added another If statement inside the action portion of the ElseIf, and tested for $z -eq 3.

$y = 5; $z = 3

If ($y -eq 5 -and $z -eq 2) {
    Write-Output -InputObject 'If'
} ElseIf ($y -eq 5) {
    Write-Output -InputObject 'ElseIf 1' # Exits If-ElseIf.
} ElseIf ($z -eq 3) {
    Write-Output -InputObject 'ElseIf 2'
}

ElseIf 1

We’re going to end here with the If language construct, and start fresh with the Switch statement next Monday. I encourage you to ask any questions you might have about the If statement, as it’s vital you have a good understanding of this introductory language command.

Keep in mind the examples could’ve been much different than just comparing numbers. We could have return services and taken action based on whether a service was located, or not, or running or stopped. We could have returned a specific Active Directory user and taken action based on whether we found the user, or not. The list goes on.

Leave a Reply

Your email address will not be published. Required fields are marked *