How to Use If Statements in PowerShell –

[ad_1]

When you are starting out learning how to create PowerShell scripts to accomplish responsibilities for you it is definitely thrilling when you see your script operate the way it should. Now it is time to choose it to the subsequent level and give your script the capability to make conclusions employing conditional logic statements. The PowerShell if assertion assemble is a typical way to determine problems in your script. If statements in PowerShell mimic the conclusion-earning approach persons use every working day. If a ailment is met, then some thing occurs. For instance, if it’s raining outdoors, I’ll seize an umbrella right before heading outside the house.

powershell if statements 1

In this diagram, if the problem is accurate, then it runs a unique command or assertion. If the condition is wrong, it moves on to the future command or assertion. Here’s a straightforward PowerShell example.

If statements in PowerShell

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

if (situation) assertion or command

or

    $situation = $accurate
    if ( $condition )
    
        Generate-Output "The situation was true"
    

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

Comparison operators

The most common issue you will use if statements in PowerShell are for evaluating two merchandise with just about every other. Powershell has distinctive operators for various comparison situations. When you use a comparison operator, the value on the still left-hand side is when compared to the price on the proper-hand side.

The -eq does equality checks among two values to make confident they are equal to every single other.

    $price = Get-MysteryValue
    if ( 5 -eq $value )
    
        # do one thing
    

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

Other operator’s values that can be applied –

Operator Comparison
-eq equals
-ne not equals
-gt greater than
-ge higher than or equal
-lt much less than
-le a lot less than or equivalent
-like string matches wildcard pattern
-notlike string does not match wildcard sample
-match string matches regex pattern
-notmatch string does not match regex pattern
-includes assortment incorporates a vlaue
-notcontains selection does not comprise a worth
-in price is in a assortment
-notin worth is not in a collection
-is both objects are the identical variety
-isnot the objects are not the identical sort

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

Now that we have protected how the If assertion will work, I would like to show you a widespread use case I have made use of the If Assertion several instances just before.

I frequently find myself producing scripts that I would only like to run if a certain file exists or does not exist.

For instance, this is good if you want to run a script if an software is installed simply because a selected file will exist on a computer system.

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

Examination-Path -Route c:reportsReport1.txt

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

If (Check-Path -Path E:reportsprocesses.txt ) 
Duplicate-Product -Path E:reportsprocesses.txt -Vacation spot C:studies

In this case in point, I will check 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 work.

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

In the last sub-segment, you observed how to look at if a file exists and duplicate the file. What if you want to copy the file rather?

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

Listed here is the updated script that works by using PowerShell “IF” assertion to verify if a file exists. Then, if it exists, delete it…

$fileexists = Test-Path -Route E:reportsfirst-file.txt
 If ($fileexists ) 
 Take out-Product -Path E:reportsfirst-file.txt -Force
 

Closing

PowerShell is an extremely powerful device that every single sysadmin must be employing. The if statement is these a simple assertion but is a incredibly essential piece of PowerShell, permitting you to automate sophisticated duties based mostly and conditional conclusion-generating. You will discover by yourself utilizing this many occasions in nearly each script you publish. I hope this posting has presented you a better comprehension than you had ahead of.

[ad_2]

Source backlink