How to Use If Statements in PowerShell –

When you are setting up out understanding how to publish PowerShell scripts to perform duties for you it is truly interesting when you see your script operate the way it need to. Now it is time to acquire it to the upcoming level and give your script the capacity to make decisions making use of conditional logic statements. The PowerShell if assertion construct is a frequent way to define problems within your script. If statements in PowerShell mimic the decision-earning method folks use each individual day. If a situation is fulfilled, then anything occurs. For illustration, if it is raining outdoors, I’ll seize an umbrella ahead of heading outdoors.

powershell if statements 1

In this diagram, if the situation is legitimate, then it runs a certain command or statement. If the issue is wrong, it moves on to the future command or statement. Here’s a very simple PowerShell example.

If statements in PowerShell

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

if (situation) assertion or command

or

    $problem = $accurate
    if ( $ailment )
    
        Generate-Output "The problem was legitimate"
    

The initially point the if statement does is assess the expression in parentheses. If it evaluates to $accurate, then it will execute the scriptblock in the braces. If the price was $fake, then it would skip over that scriptblock.

Comparison operators

The most popular factor you will use if statements in PowerShell are for evaluating two goods with just about every other. Powershell has exclusive operators for distinctive comparison scenarios. When you use a comparison operator, the value on the remaining-hand aspect is in comparison to the price on the appropriate-hand side.

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

    $price = Get-MysteryValue
    if ( 5 -eq $worth )
    
        # do anything
    

In this instance, I am having a recognized value of 5 and comparing it to my $price to see if they match.

Other operator’s values that can be applied –

Operator Comparison
-eq equals
-ne not equals
-gt increased than
-ge increased than or equivalent
-lt a lot less than
-le considerably 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 pattern
-includes collection consists of a vlaue
-notcontains assortment does not include a benefit
-in benefit is in a assortment
-notin value is not in a assortment
-is both equally objects are the same sort
-isnot the objects are not the same sort

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

Now that we have lined how the If assertion performs, I would like to present you a prevalent use case I have applied the If Statement quite a few instances in advance of.

I usually locate myself producing scripts that I would only like to operate if a distinct file exists or does not exist.

For illustration, this is excellent if you want to run a script if an application is put in because a particular file will exist on a computer.

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

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

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

If (Check-Route -Path E:reportsprocesses.txt ) 
Duplicate-Product -Route E:reportsprocesses.txt -Place C:stories

In this illustration, I will check if “c:reportsReport1.txt” exists and if it exists, I will copy the file to “C:reports”. In this article is the script that will do the occupation.

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

In the very last sub-area, you saw how to verify if a file exists and duplicate the file. What if you want to copy the file instead?

If you want to delete the file instead of copying it, exchange the Duplicate-Product command with the Take away-Merchandise command.

Right here is the up to date script that utilizes PowerShell “IF” assertion to examine if a file exists. Then, if it exists, delete it…

$fileexists = Check-Route -Path E:reportsfirst-file.txt
 If ($fileexists ) 
 Get rid of-Merchandise -Route E:reportsfirst-file.txt -Drive
 

Closing

PowerShell is an incredibly potent tool that each and every sysadmin should really be making use of. The if statement is this kind of a basic assertion but is a quite basic piece of PowerShell, making it possible for you to automate elaborate tasks primarily based and conditional determination-creating. You will uncover yourself employing this several occasions in practically each script you publish. I hope this post has given you a better knowledge than you had prior to.