PowerShell IF ELSE: Unleashing the power of conditionals

powershellifelsePowerShell is a powerful language designed by Microsoft to enable remote administration and control of Windows machines. If you’re a system adminstrator, you’ve probably worked with PowerShell before. If you’re an aspiring system administrator, dont worry, it’s easy to learn programming in Powershell.

PowerShell lets you remotely manage computers to view and modify registries of remote machines, and even kill pesky processes that won’t close through normal stop methods. Today, we’re going to take a look at the “if… else” conditional statement in PowerShell. We’re assuming that you have the latest version of PowerShell installed on your PC. If you don’t, you can download it at the Microsoft.com website. Make sure you have .net Framework 4.0 installed too, as PowerShell 3.0 is based on the .net Framework.

IF conditional statement in PowerShell

Before we get started on the “if…else” conditional statement, you first need to understand the “if” conditional statement. In modern programming languages, conditional statements tell the language to execute a block of code if a certain condition is met. If a condition is met, PowerShell will receive a “True” Boolean value and begin to execute the following block of code. If it receives a “False” Boolean value, it will skip the block of code inside the conditional statement.

The syntax for the “if” statement in PowerShell is:

If (your condition) {code to be executed}

Example: Let’s write a simple program that demonstrates the “if” conditional statement:

$i = 2
if ($i –lt 3) {Write-Host “A simple if program.”}
Output: A simple if program

First, we initialized the value of “i” as 2. Later, we set the condition that if the value of “i” is less than (-lt) 3, print “A simple if program” on the screen.

Comparison and Logical Operators in PowerShell

PowerShell has several comparison operators and logical operators that you can use to create your conditional statements.  Comparison operators let you do mathematical kind of operations.  Here’s a short list of the common comparison operators and logical operators.

Comparison Operators
–eq (equal to)
-gt (greater than)
-lt (less than)
-le (less than or equal to)
-ge (greater than or equal to)
-ne (not equal to)
Logical operators 

If…else conditional statement in PowerShell

You can use the “if…else” conditional statement in PowerShell if you have two blocks of code and you need to execute either one of the two if a certain condition is met. The syntax for the “if…else” statement is:

if (your condition) { first chunk of code}
else (your condition) {second chunk of code }

PowerShell will first check the Boolean value of the first condition (specified after if). If it’s true, the code covered by the “if” statement will get executed. If the value turns out to be false, the code covered by the “else” statement will get executed instead.

Example Here’s a simple program to demonstrate the “if…else” conditional statement. In practice, you can tack on the “else” statement almost after every “if” statement:

$i = 2
if ($i –lt 2) {Write-Host “A simple if program.”}
else ($i –lt 3) {Write-Host “A simple if…else program.”}
Output: A simple if…else program.

Because the value of “i” is equal to, and not less than, 2, PowerShell will skip the code following the if statement and execute the code following the else statement, where its condition has been met.

Example  You can omit specifying the condition for the “else” statement:

$i = 2
if ($i –lt 2) {Write-Host “A simple if program.”}
else {Write-Host “A simple if…else program, with no else conditional.”}
Output: A simple if…else program, with no else conditional.

Here, PowerShell will simply assume the condition following the else statement has been met and print “A simple if…else program, with no conditional”.

Example  Here’s a program to help you understand the limitations of an “if” conditional statement:

$i = 2
if ($i –le 2) {Write-Host “A simple if program.”}
else ($i –le2) {Write-Host “A simple if…else program.”}
Output: A simple if program.

You can see that both the “if” and the “else” statement satisfy the condition that “i” is less than or equal to 2. So why doesn’t PowerShell print “A simple if…else program” on screen? Because once it finds that the “if” condition has been satisfied, it will skip the succeeding “else” condition. You cannot get PowerShell to execute both blocks of code using the “if…else” statement. PowerShell provides the “switch” statement for that purpose.

Combining “if…else” with Logical Operators in PowerShell

You can use the –not (!), -and and –or operators with your “if…else” statement to combine multiple conditionals. Here’s an example to show how to execute a block of code if two conditions have to be met

$i = 2
if (($i –le 2) –and ($i ile2)) {Write-Host “A simple if program.”}
else ($i –le2) {Write-Host “A simple if…else program.”}
Output: A simple if program.

Here, we’ve used the –and operator to assign two different conditions for PowerShell within the “if” condition section. You have to enclose the conditions with brackets separately “()” to get PowerShell to register them.

Using “if…else” for Practical Purposes

There are several applications for the “if…else” statement on your PC. For example, you can use it to quickly find out if a file or a folder exists or not at a certain location in your hard drive by combining it with the Test-Path cmdlet. The Test-Path cmdlet returns a true value if it finds a certain file (or folder) exists and a false value if it doesn’t. You can even use Test-Path to check for registry keys! Here’s an example to help you understand:

$filecheck = “D:\Games\solitaire.exe”
$filepresent = Test-Path $filecheck
If ($filepresent –eq $True) {Write-Host “Solitaire is present”}
else {Write-Host “File isn’t present”}

Output: Now, depending on whether the file is present in “D:\Games”, the output will be printed as “Solitaire is present” or “File isn’t present”.

You can use conditional statements in PowerShell (if, if…else, ElseIf, if –not, if, -or) to make intelligent scripts for different kind of decision-making. You can use them to get an application to stop running, to run an application, for security and for a host of other purposes. We’ve just shown you the tip of the iceberg. The only way you for you to familiarise yourself with PowerShell is to practice as much as possible!