How to Use If Statements in PowerShell –

When you are starting up out understanding how to compose PowerShell scripts to accomplish duties for you it is genuinely enjoyable when you see your script perform the way it need to. Now it is time to acquire it to the subsequent degree and give your script the capability to make choices employing conditional logic statements. The PowerShell if statement assemble is a widespread way to outline ailments in just your script. If statements in PowerShell mimic the choice-generating course of action folks use every day. If a issue is met, then a thing happens. For instance, if it is raining outside, I’ll get an umbrella in advance of heading outdoors.

powershell if statements 1

In this diagram, if the ailment is real, then it runs a particular command or statement. If the problem is wrong, it moves on to the future command or statement. Here’s a uncomplicated PowerShell example.

If statements in PowerShell

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

if (condition) statement or command

or

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

The 1st detail the if statement does is examine the expression in parentheses. If it evaluates to $true, then it will execute the scriptblock in the braces. If the price was $untrue, then it would skip around that scriptblock.

Comparison operators

The most popular thing you will use if statements in PowerShell are for evaluating two merchandise with every other. Powershell has exclusive operators for distinct comparison scenarios. When you use a comparison operator, the value on the still left-hand aspect is in comparison to the benefit on the suitable-hand facet.

The -eq does equality checks between two values to make guaranteed they are equivalent to just about every other.

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

In this instance, I am using a known benefit 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 better than
-ge better than or equivalent
-lt less than
-le a lot less than or equivalent
-like string matches wildcard sample
-notlike string does not match wildcard sample
-match string matches regex sample
-notmatch string does not match regex sample
-includes assortment incorporates a vlaue
-notcontains collection does not comprise a value
-in price is in a collection
-notin worth is not in a selection
-is each objects are the very same style
-isnot the objects are not the same form

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

Now that we have protected how the If assertion is effective, I would like to show you a frequent use situation I have utilised the If Assertion many periods ahead of.

I frequently discover myself building scripts that I would only like to run if a individual file exists or does not exist.

For example, this is terrific if you want to run a script if an software is mounted because a particular file will exist on a laptop or computer.

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

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

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

If (Check-Path -Path E:reportsprocesses.txt ) 
Duplicate-Merchandise -Path E:reportsprocesses.txt -Place C:experiences

In this illustration, I will examine if “c:reportsReport1.txt” exists and if it exists, I will duplicate the file to “C:reports”. Here is the script that will do the task.

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

In the very last sub-part, you saw how to look at if a file exists and copy the file. What if you want to copy the file alternatively?

If you want to delete the file as a substitute of copying it, substitute the Copy-Product command with the Take out-Merchandise command.

Right here is the updated script that takes advantage of PowerShell “IF” statement to check out if a file exists. Then, if it exists, delete it…

$fileexists = Examination-Route -Path E:reportsfirst-file.txt
 If ($fileexists ) 
 Remove-Product -Path E:reportsfirst-file.txt -Drive
 

Closing

PowerShell is an incredibly highly effective device that each individual sysadmin ought to be applying. The if statement is such a uncomplicated statement but is a extremely elementary piece of PowerShell, allowing for you to automate sophisticated duties centered and conditional decision-earning. You will discover by yourself making use of this multiple occasions in nearly each individual script you create. I hope this posting has offered you a greater comprehending than you had just before.

Luis Robinson

Next Post

What is PowerShell Splatting? -

Sat Dec 24 , 2022
Have you at any time worked on a Powershell command that has developed to the position exactly where you pass so several parameters that studying it will become a mission? For instance, if you want to build a new consumer in Advertisement, you can simply need 10 parameters to complete […]
What is PowerShell Splatting? –

You May Like