December 5, 2022

niagaraonthemap

Simply Consistent

How to Use If Statements in PowerShell –

When you are starting out learning how to write PowerShell scripts to perform tasks for you it is really exciting when you see your script work the way it should. Now it is time to take it to the next level and give your script the ability to make decisions using conditional logic statements. The PowerShell if statement construct is a common way to define conditions within your script. If statements in PowerShell mimic the decision-making process people use every day. If a condition is met, then something happens. For example, if it’s raining outside, I’ll grab an umbrella before heading outside.

powershell if statements 1

In this diagram, if the condition is true, then it runs a specific command or statement. If the condition is false, it moves on to the next command or statement. Here’s a simple PowerShell example.

If statements in PowerShell

The syntax of If statements in PowerShell is pretty basic and resembles other coding languages.

if (condition) statement or command

or

    $condition = $true
    if ( $condition )
    
        Write-Output "The condition was true"
    

The first thing the if statement does is evaluate the expression in parentheses. If it evaluates to $true, then it will execute the scriptblock in the braces. If the value was $false, then it would skip over that scriptblock.

Comparison operators

The most common thing you will use if statements in PowerShell are for comparing two items with each other. Powershell has special operators for different comparison scenarios. When you use a comparison operator, the value on the left-hand side is compared to the value on the right-hand side.

The -eq does equality checks between two values to make sure they are equal to each other.

    $value = Get-MysteryValue
    if ( 5 -eq $value )
    
        # do something
    

In this example, I am taking a known value of 5 and comparing it to my $value to see if they match.

Other operator’s values that can be used –

Operator Comparison
-eq equals
-ne not equals
-gt greater than
-ge greater than or equal
-lt less than
-le less than or equal
-like string matches wildcard pattern
-notlike string does not match wildcard pattern
-match string matches regex pattern
-notmatch string does not match regex pattern
-contains collection contains a vlaue
-notcontains collection does not contain a value
-in value is in a collection
-notin value is not in a collection
-is both objects are the same type
-isnot the objects are not the same type

How to Use If Statements in PowerShell to Check If A File Exists

Now that we have covered how the If statement works, I would like to show you a common use case I have used the If Statement many times before.

I often find myself creating scripts that I would only like to run if a particular file exists or does not exist.

For example, this is great if you want to run a script if an application is installed because a certain file will exist on a computer.

The statement that you can use to see if a file exists is the test-path statement.

Test-Path -Path c:\reports\Report1.txt

If the file exists the Output “True” will be displayed

If (Test-Path -Path E:\reports\processes.txt ) 
Copy-Item -Path E:\reports\processes.txt -Destination C:\reports

In this example, I will check if “c:\reports\Report1.txt” exists and if it exists, I will copy the file to “C:\reports”. Here is the script that will do the job.

How To UseIf Statements in PowerShell To Check If A File Exists And Delete It

In the last sub-section, you saw how to check if a file exists and copy the file. What if you want to copy the file instead?

If you want to delete the file instead of copying it, replace the Copy-Item command with the Remove-Item command.

Here is the updated script that uses PowerShell “IF” statement to check if a file exists. Then, if it exists, delete it…

$fileexists = Test-Path -Path E:\reports\first-file.txt
 If ($fileexists ) 
 Remove-Item -Path E:\reports\first-file.txt -Force
 

Closing

PowerShell is an extremely powerful tool that every sysadmin should be using. The if statement is such a simple statement but is a very fundamental piece of PowerShell, allowing you to automate complex tasks based and conditional decision-making. You will find yourself using this multiple times in almost every script you write. I hope this article has given you a better understanding than you had before.