PowerShell Pipeline

PowerShell Basics: Working with the If Statement

Just like any good tool, knowing the different ways to use the If statement will help your script in many ways.

Every programming and scripting language has had some sort of way to perform a conditional operation using an If statement as part of flow control in a script or program. PowerShell is definitely no different and I am going to take some time to cover the basics of PowerShell by looking at the If/Else/ElseIf statements to help you know when and where to use them.

Working in PowerShell, there will be times that you need to perform some sort of comparison to determine what kind of action should take place. To do that one possibly option is to look at using an If statement and also an Else statement that usually will accompany it to handle the result that doesn't occur with the If. Sometimes, there will be more than one expected outcome based on the comparison. If that is the case, you can make use of multiple ElseIf statements to handle each of those.

How this works is that you take your item that you want to compare against another item and use a comparison operator such as –eq (this means that you want to see if something is equal to whatever you are comparing it against; more on these later) which then returns a Boolean value of True (meets the comparison) or False (does not meet the comparison).

A quick example that shows this in action will simply look at if a number is 1 or isn't 1.

If (1 -eq 1) {
  'This number is  1'
} Else {
'This number is not 1'

The result of what will happen should seem pretty obvious. We will always get the string that says the value is 1. If the number was anything else other than 1, then we would get back the string from the Else block stating that the value is not 1.

Remember, anything that goes in the script block after the If statement should always be True -- unless you expect the opposite using the –NOT operator in the If statement as shown below.

If (-NOT  (1 -eq 1)) {
'This number is not 1'
} Else {
'This number is 1'   

Adding the –NOT means that the opposite is true so now the Else script block will be run when we execute the code above.

So far we have looked at simple If/Else statements where only one thing was being looked at. We can easily string together multiple comparisons and then provide the proper script block to handle what happens when the result is either true or false.

$Data1 = 'Apple'
$Data2 = 'Red'

If ($Data1 -eq 'Apple' -AND $Data2 -eq 'Red') {
'This is a red apple'

If ($Data1 -eq 'Banana' -AND $Data2 -eq 'Red') {
'This is a red apple'
Figure 1. Example of where an If would show output as well as not showing output.

Here we see that given $Data1 is Apple and $Data2 is red, that the first script example comes back stating that 'This is a red apple' as both of the comparisons are true while the second example does not output anything because it did not meet the requirements of the If statement ($Data1 is not 'Banana') even though one of the variables was true ($Data2 is 'red'). This is important when you are troubleshooting the flow of your script and are wondering why the proper script block is not being run in your If/Else block. The use of the –AND operator means that both of these values must be $True with their respective comparisons before it will let me use the If script block. If that wasn't the case, then the script block for Else would have been used instead. If we only care about one of the two (or both still) comparison being True, we could change out the –AND with an –OR and as long as one of the results is true, the script block for If would run.

I mentioned using ElseIf earlier and now we can take a look at using it see how we can use it to handle more than 2 possible outcomes with our flow control.

$Data = 'Apple'
If ($Data -eq 'orange') {
'This is an orange'
} ElseIf ($Data -eq 'grape') {
'This is a grape'
} ElseIf ($Data -eq 'Apple') {
'This is an apple'
} Else {
'Cannot determine what this is'

In this example, we know that the result will be that the data is an Apple, but unlike before where we only had two options, here we have multiple options and finally our Else which is the catch-all if none of the other ElseIfs are able to handle the comparisons. One last example of this is below:

$Data = 1..7
$Data | ForEach {
If ([int]$_ -gt 3) {
"$($_) is greater than 3"
} ElseIf ([int]$_ -lt 3) {
"$($_) is less than 3"
} ElseIf ([int]$_ -eq 3) {
"$($_) is 3"
} Else {
"No idea what this is; not a number"
Figure 2. Another look at using If/ElseIf/Else flow control.

Using the ElseIf, we can handle whether a number is larger, smaller or equal to whatever we throw at it and am able to handle it accordingly.

As you noticed, I threw out a –gt (greater than) and –lt (less than) as possible comparison operators. In fact, there are several operators that we can use to perform a comparison.


Equals to


Not equal to


Greater than


Greater than or equal to


Less than


Less than or equal to


Wildcard match


Does not match wildcard


Regular expression matching


Does not match regular expression patter


Collection contains item


Collection does not contain item


Item is in a collection


Item is not in a collection

Now, I am not going to go through all of these with examples, but I encourage you to look at the help documentation on comparison operators to better familiarize yourself with these so you can know where you can use them at.

Get-Help about_Comparison_Operators 

Now that you have a better understanding of flow control using If/Else/ElseIf, you can apply this to your scripts to better handle the data that is being sent to it.

About the Author

Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.

comments powered by Disqus
Most   Popular