How to Use If Statements in PowerShell –

When you are commencing out understanding how to write PowerShell scripts to perform tasks for you it is actually thrilling when you see your script do the job the way it really should. Now it is time to acquire it to the next amount and give your script the skill to make choices utilizing conditional logic statements. The PowerShell if statement build is a prevalent way to determine circumstances inside of your script. If statements in PowerShell mimic the decision-creating process persons use every day. If a problem is fulfilled, then a thing occurs. For instance, if it is raining outdoors, I’ll grab an umbrella prior to heading outside.

powershell if statements 1

In this diagram, if the affliction is genuine, then it operates a particular command or assertion. If the condition is wrong, it moves on to the future command or assertion. Here’s a very simple PowerShell example.

If statements in PowerShell

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

if (ailment) statement or command

or

    $situation = $legitimate
    if ( $situation )
    
        Publish-Output "The issue was accurate"
    

The to start with detail the if statement does is consider the expression in parentheses. If it evaluates to $correct, then it will execute the scriptblock in the braces. If the benefit was $wrong, then it would skip around that scriptblock.

Comparison operators

The most prevalent issue you will use if statements in PowerShell are for comparing two objects with every other. Powershell has distinctive operators for distinctive comparison situations. When you use a comparison operator, the price on the left-hand aspect is in contrast to the benefit on the suitable-hand aspect.

The -eq does equality checks amongst two values to make confident they are equal to just about every other.

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

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

Other operator’s values that can be utilized –

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

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

Now that we have included how the If statement works, I would like to present you a typical use case I have utilised the If Statement numerous moments prior to.

I typically obtain myself producing scripts that I would only like to operate if a specific file exists or does not exist.

For example, this is terrific if you want to operate a script if an application is mounted due to the fact a certain file will exist on a computer.

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

Exam-Route -Route c:reportsReport1.txt

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

If (Take a look at-Path -Route E:reportsprocesses.txt ) 
Duplicate-Merchandise -Path E:reportsprocesses.txt -Spot C:stories

In this illustration, I will verify if “c:reportsReport1.txt” exists and if it exists, I will duplicate the file to “C:reports”. In this article 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-segment, you saw how to test if a file exists and copy the file. What if you want to duplicate the file rather?

If you want to delete the file as an alternative of copying it, replace the Duplicate-Merchandise command with the Get rid of-Merchandise command.

Below is the up to date script that works by using PowerShell “IF” statement to examine if a file exists. Then, if it exists, delete it…

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

Closing

PowerShell is an really effective instrument that every single sysadmin ought to be making use of. The if statement is these types of a basic assertion but is a pretty elementary piece of PowerShell, allowing for you to automate complex duties based and conditional choice-generating. You will locate you making use of this a number of instances in just about each and every script you create. I hope this report has specified you a much better comprehending than you experienced just before.

Next Post

What is PowerShell Splatting? -

Have you ever worked on a Powershell command that has grown to the level wherever you go so quite a few parameters that reading it gets to be a mission? For illustration, if you want to create a new user in Advert, you can quickly need 10 parameters to perform […]
What is PowerShell Splatting? –