Conditional constructs like if/then have been in programming languages for decades. Eventually, if a script or application goes past a couple of lines you're going to ultimately need to make a decision based on the condition. The PowerShell if statement is no different from if statements in other languages. It's simply a way for your script to decide which way to go based on a condition at some point in the script. Think of an if statement in PowerShell like a script crossroads of sorts.

Let's say you've encountered a problem that you'd like to write a PowerShell script to solve. A typical task might be to read a text file with a list of server names and, based on their name, do some kind of action on them.

Did you notice my phrase "based on their name"? This is a clear indicator for the need of an if statement. Let's write the script.

$Servers = Get-Content C:\MyServers.txt
foreach ($Server in $Servers) {
    if ($Server -like '*-DC'') {
        ## Do something Active Directory-related here
    } else {
        ## Do some general server thing here
    }
} 

This small script reads a text file of server names (one per line) and places all of those servers into a variable called $Servers. It then reads each of those server names calling each server name it processes the variable name $Server. Inside this loop is where our if statement comes into play.

I want to only perform some Active Directory function if the server name ends with the characters "-DC". I'm doing this because I always name my domain controllers ending with "-DC".

As my script reads each server in the list it will get to that "crossroads" at line 3 and ask a question. Does the server name I'm looking at right now end with the characters "-DC"? It then performs an action based on that question's answer. If the server name does end in "-DC" then the if statement does some Active Directory stuff else it does some general server stuff.

This is the most simple form of the Powershell if statement. It's possible you may have multiple conditions you need to account for as well. Let's say you've got some SQL servers in that list too.

$Servers = Get-Content C:\MyServers.txt
foreach ($Server in $Servers) {
    if ($Server -like '*-DC'') {
        ## Do something Active Directory-related here
    } elseif ($Server -like '*-SQL') {
        ## Do some SQL stuff here
    } else {
        ## Do some general server thing here
    }
}

Notice how I just added another if statement in there? To add a new condition it's just a matter of appending another if statement to the end of the else construct on line 5.

It will perform the exact same way as the original if statement did but only this time it's checking to see if the server names end in "-SQL" instead of "-DC". You can add all the conditions you want but if you find yourself adding more than 2-3 conditions it's wise to start looking at the PowerShell switch statement which I may go over in an upcoming article.

The PowerShell if statement is one of the most basic and important aspects of PowerShell that you need to learn. If you end up writing a lot of PowerShell scripts you'll find yourself coming back to the if/then construct more and more so it's very important you grasp this concept.

If you do learn this then you're on your way to Powershell greatness else you'll never get past writing 1-line scripts.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!