February 6, 2023

excellentpix

Unlimited Technology

How to Use If Statements in PowerShell –

When you are starting up out understanding how to compose PowerShell scripts to carry out duties for you it is genuinely exciting when you see your script operate the way it must. Now it is time to just take it to the subsequent level and give your script the means to make decisions making use of conditional logic statements. The PowerShell if statement construct is a prevalent way to outline ailments inside your script. If statements in PowerShell mimic the decision-building approach persons use every day. If a affliction is satisfied, then anything occurs. For case in point, if it’s raining exterior, I’ll grab an umbrella right before heading outside.

powershell if statements 1

In this diagram, if the situation is legitimate, then it runs a precise command or statement. If the condition is fake, it moves on to the following command or assertion. Here’s a straightforward PowerShell example.

If statements in PowerShell

The syntax of If statements in PowerShell is rather basic and resembles other coding languages.

if (situation) assertion or command

or

    $ailment = $correct
    if ( $ailment )
    
        Write-Output "The issue was correct"
    

The to start with matter the if statement does is assess the expression in parentheses. If it evaluates to $correct, then it will execute the scriptblock in the braces. If the benefit was $fake, then it would skip over that scriptblock.

Comparison operators

The most common detail you will use if statements in PowerShell are for comparing two merchandise with every other. Powershell has distinctive operators for distinct comparison scenarios. When you use a comparison operator, the value on the remaining-hand facet is compared to the value on the correct-hand facet.

The -eq does equality checks concerning two values to make guaranteed they are equal to each other.

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

In this case in point, I am taking a recognized value of 5 and comparing it to my $worth to see if they match.

Other operator’s values that can be utilised –

Operator Comparison
-eq equals
-ne not equals
-gt larger than
-ge bigger than or equal
-lt significantly less than
-le fewer 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
-is made up of assortment is made up of a vlaue
-notcontains assortment does not incorporate a price
-in price is in a collection
-notin benefit is not in a assortment
-is both objects are the very same kind
-isnot the objects are not the very same kind

How to Use If Statements in PowerShell to Look at If A File Exists

Now that we have lined how the If assertion will work, I would like to exhibit you a common use circumstance I have utilised the If Assertion a lot of situations prior to.

I usually locate myself building scripts that I would only like to run if a specific file exists or does not exist.

For illustration, this is terrific if you want to run a script if an application is set up mainly because a sure file will exist on a computer system.

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

Check-Path -Path c:reportsReport1.txt

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

If (Take a look at-Route -Route E:reportsprocesses.txt ) 
Duplicate-Item -Path E:reportsprocesses.txt -Desired destination C:reports

In this illustration, I will examine 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 task.

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

In the last sub-segment, you saw how to examine if a file exists and duplicate the file. What if you want to duplicate the file alternatively?

If you want to delete the file in its place of copying it, swap the Copy-Item command with the Take away-Product command.

In this article is the up-to-date script that makes use of PowerShell “IF” statement to check out if a file exists. Then, if it exists, delete it…

$fileexists = Take a look at-Route -Route E:reportsfirst-file.txt
 If ($fileexists ) 
 Clear away-Merchandise -Path E:reportsfirst-file.txt -Drive
 

Closing

PowerShell is an extremely strong resource that every sysadmin really should be making use of. The if statement is these a easy assertion but is a quite elementary piece of PowerShell, allowing you to automate complex tasks based mostly and conditional final decision-earning. You will obtain oneself working with this a number of periods in practically every single script you create. I hope this article has given you a improved comprehending than you experienced right before.