Tech Guru

Trusted Source Technology

How to Use If Statements in PowerShell –

How to Use If Statements in PowerShell –

When you are starting out learning how to compose PowerShell scripts to carry out responsibilities for you it is genuinely exciting when you see your script do the job the way it need to. Now it is time to acquire it to the upcoming level and give your script the means to make decisions using conditional logic statements. The PowerShell if assertion assemble is a prevalent way to outline circumstances inside of your script. If statements in PowerShell mimic the conclusion-making process folks use each individual working day. If a condition is achieved, then a thing takes place. For case in point, if it is raining outdoors, I’ll grab an umbrella ahead of heading exterior.

powershell if statements 1

In this diagram, if the problem is real, then it runs a specific command or statement. If the affliction is untrue, it moves on to the up coming command or statement. Here’s a simple PowerShell example.

If statements in PowerShell

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

if (situation) assertion or command


    $issue = $accurate
    if ( $ailment )
        Create-Output "The ailment was legitimate"

The initially factor the if statement does is appraise the expression in parentheses. If it evaluates to $genuine, then it will execute the scriptblock in the braces. If the price was $fake, then it would skip in excess of that scriptblock.

Comparison operators

The most widespread point you will use if statements in PowerShell are for evaluating two objects with each and every other. Powershell has special operators for distinctive comparison scenarios. When you use a comparison operator, the benefit on the still left-hand side is as opposed to the price on the proper-hand aspect.

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

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

In this case in point, I am having a recognised value of 5 and comparing it to my $value to see if they match.

Other operator’s values that can be utilised –

Operator Comparison
-eq equals
-ne not equals
-gt higher than
-ge better than or equal
-lt a lot less than
-le a lot less than or equivalent
-like string matches wildcard pattern
-notlike string does not match wildcard pattern
-match string matches regex pattern
-notmatch string does not match regex sample
-contains assortment incorporates a vlaue
-notcontains assortment does not incorporate a benefit
-in worth is in a selection
-notin worth is not in a collection
-is both of those objects are the very same sort
-isnot the objects are not the same kind

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

Now that we have covered how the If statement operates, I would like to demonstrate you a widespread use scenario I have utilised the If Statement many situations before.

I often locate myself building scripts that I would only like to run if a certain file exists or does not exist.

For case in point, this is fantastic if you want to operate a script if an application is installed because a particular file will exist on a laptop.

The statement that you can use to see if a file exists is the take a look at-route statement.

Take a look at-Route -Route c:reportsReport1.txt

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

If (Test-Path -Path E:reportsprocesses.txt ) 
Duplicate-Item -Route E:reportsprocesses.txt -Place C:experiences

In this example, I will look at if “c:reportsReport1.txt” exists and if it exists, I will copy the file to “C:reports”. Below is the script that will do the occupation.

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

In the very last sub-area, you noticed how to examine if a file exists and duplicate the file. What if you want to duplicate the file rather?

If you want to delete the file as a substitute of copying it, swap the Duplicate-Product command with the Get rid of-Product command.

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

$fileexists = Test-Path -Path E:reportsfirst-file.txt
 If ($fileexists ) 
 Remove-Product -Path E:reportsfirst-file.txt -Power


PowerShell is an extremely highly effective instrument that every single sysadmin ought to be making use of. The if statement is such a simple statement but is a very elementary piece of PowerShell, allowing for you to automate sophisticated duties primarily based and conditional choice-producing. You will discover you applying this multiple instances in nearly each individual script you create. I hope this posting has specified you a better comprehension than you experienced just before.