April 19, 2024


Technology can't be beat

How to Use If Statements in PowerShell –


When you are starting off out learning how to publish PowerShell scripts to execute tasks for you it is genuinely exciting when you see your script work the way it should really. Now it is time to choose it to the future degree and give your script the means to make conclusions working with conditional logic statements. The PowerShell if statement assemble is a popular way to outline disorders in your script. If statements in PowerShell mimic the final decision-making method folks use each day. If a affliction is satisfied, then some thing occurs. For case in point, if it’s raining outside the house, I’ll grab an umbrella ahead of heading outside.

powershell if statements 1

In this diagram, if the problem is correct, then it operates a unique command or assertion. If the problem is bogus, it moves on to the up coming command or statement. Here’s a straightforward PowerShell case in point.

If statements in PowerShell

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

if (problem) assertion or command


    $issue = $correct
    if ( $condition )
        Create-Output "The ailment was true"

The initially factor the if statement does is assess 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 issue you will use if statements in PowerShell are for comparing two objects with just about every other. Powershell has special operators for various comparison situations. When you use a comparison operator, the worth on the still left-hand facet is as opposed to the worth on the appropriate-hand aspect.

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

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

In this illustration, I am taking a recognised price of 5 and comparing it to my $worth to see if they match.

Other operator’s values that can be used –

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

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

Now that we have included how the If statement performs, I would like to show you a widespread use situation I have utilized the If Statement many times prior to.

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

For case in point, this is great if you want to run a script if an software is mounted for the reason that a particular file will exist on a laptop or computer.

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

Examination-Route -Path c:reportsReport1.txt

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

If (Test-Path -Path E:reportsprocesses.txt ) 
Copy-Merchandise -Route E:reportsprocesses.txt -Place C:reviews

In this case in point, I will check out if “c:reportsReport1.txt” exists and if it exists, I will duplicate the file to “C:reports”. Below is the script that will do the position.

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

In the very last sub-part, you observed how to check if a file exists and duplicate the file. What if you want to duplicate the file rather?

If you want to delete the file in its place of copying it, replace the Duplicate-Item command with the Get rid of-Product command.

Here is the up-to-date script that employs PowerShell “IF” statement to check out if a file exists. Then, if it exists, delete it…

$fileexists = Check-Path -Route E:reportsfirst-file.txt
 If ($fileexists ) 
 Take out-Item -Path E:reportsfirst-file.txt -Drive


PowerShell is an very powerful tool that each and every sysadmin should be using. The if statement is such a very simple statement but is a very elementary piece of PowerShell, making it possible for you to automate intricate jobs based and conditional determination-producing. You will find you employing this a number of situations in pretty much every single script you compose. I hope this post has provided you a greater being familiar with than you experienced ahead of.


Source website link