How to Use PowerShell Conditional Statements

This tutorial shows you how to use powershell conditional statements to perform tasks on your computer.

How to Use PowerShell Conditional Statements

PowerShell is a scripting language that can allow you to automate many tasks on your computer. One of the cool features of PowerShell is the ability to make decisions based on certain conditions. If you're new to PowerShell, you may not know how to use conditional statements.

Conditional statements are used all over PowerShell, and learning how to use them is essential for becoming a PowerShell expert. This post shows you how to use PowerShell's built-in If, Else, and Switch keywords to create your conditional statements. We will also see some practical examples of how you can use these statements in your scripts.

Let's get started!

Powershell Comparison Operators

Mostly conditional statements are used together with comparison operators. These operators help test whether two values are equal, unequal, greater than, less than, etc. The most common comparison operators are listed below:

Operator Functionality
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to

Note:

In powershell, we don't use mathematical symbols for evaluation like we do in other languages.

Powershell if statement

The "If" statement is the most basic of the PowerShell conditional statements. It evaluates a condition, and if it is TRUE, it will execute a code block.

To create a basic If, Else statement, all you need is the If keyword followed by a condition.

Example

Let's say we want to check if a file exists before trying to delete it. We can use the following code:

If (Test-Path C:\users\geekbits\desktop\test.txt)

{Remove-Item C:\users\geekbits\desktop\test.txt} 

Else 

{Write-Host "The file does not exist."} 

As you can see, the If keyword is followed by a set of parentheses containing our condition (Test-Path C:\temp\test.txt). In this example, we use the Test-Path cmdlet to check if the file exists. We are using the Remove-Item cmdlet to delete the file if it exists and display a message to the user if the file does not exist.

Powershell ElseIf Statement

The ElseIf statement is similar to the If statement but allows you to check multiple conditions. We will use the ElseIf statement in the following example to check for multiple log files. If one of the log files exists, we will delete it.

If (Test-Path C:\users\geekbits\desktop\test.txt) 

{Remove-Item C:\users\geekbits\desktop\test.txt} 

ElseIf C:\users\jeff\geekbits\test_01.txt) 

{Remove-Item C:\users\geekbits\desktop\test_01.log} 

ElseIf (Test-Path C:\users\geekbits\desktop\test_02.log) 

{Remove-Item C:\users\geekbits\desktop\test_02.log} 

Else 

{Write-Host "All of the log files do not exist."}

In this example, we are checking for three different log files. If any of the log files exist, we will delete them. On the other hand, if none of the log files exist, we will display a message to the user.

Powershell Else Statement

The Else statement executes a code block if all other conditions are FALSE. The following example will use the Else statement to check if a file exists. If the file does not exist, we will create it.

If (Test-Path C:\users\geekbits\desktop\test.txt) 

{Remove-Item C:\users\geekbits\desktop\test.txt} 

Else 

{New-Item -Path C:\users\geekbits\desktop\test.txt -ItemType File}

In this example, we use the Test-Path cmdlet to check if the file exists. If the file does not exist, we will use the New-Item cmdlet to create it.

Powershell Switch Statement

The Switch statement is another PowerShell conditional statement that can make more decisions. The Switch statement will evaluate and match an expression against a list of values. We will use the Switch statement in the following example to check for multiple log files. If one of the log files exists, we will delete it.

Switch ((Get-Item C:\users\geekbits\desktop\test.txt).Length) {

{$_ -gt 100KB} {Remove-Item C:\geekbits\jeff\desktop\test.txt}

{$_ -lt 100KB -And $_ -gt 20kb} {Write-Host "The file is too small!"}

Default {Write-Host "The file doesn't exist!"}

}

We are checking for the log file using the Test-Path cmdlet in this example. If the file exists, we will delete it. If the file does not exist, a message is displayed to the user.

Conclusion

These are just a few examples of using PowerShell conditional statements in your scripts. These statements can be beneficial when you need to make decisions based on certain conditions. Try using these statements in your scripts and see how they can help you automate tasks on your computer.

Thanks for reading!

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.