How one can Use If Statements in PowerShell –

If you end up beginning out studying easy methods to write PowerShell scripts to carry out duties for you it’s actually thrilling once you see your script work the best way it ought to. Now it’s time to take it to the subsequent stage and provides your script the power to make choices utilizing conditional logic statements. The PowerShell if assertion assemble is a typical option to outline situations inside your script. If statements in PowerShell mimic the decision-making course of individuals use every single day. If a situation is met, then one thing occurs. For instance, if it’s raining outdoors, I’ll seize an umbrella earlier than heading outdoors.

On this diagram, if the situation is true, then it runs a particular command or assertion. If the situation is fake, it strikes on to the subsequent command or assertion. Right here’s a easy PowerShell instance.

If statements in PowerShell

The syntax of If statements in PowerShell is fairly fundamental and resembles different coding languages.

if (situation) assertion or command

or

    $situation = $true
    if ( $situation )
    
        Write-Output "The situation was true"
    

The very first thing the if assertion does is consider the expression in parentheses. If it evaluates to $true, then it’s going to execute the scriptblock within the braces. If the worth was $false, then it will skip over that scriptblock.

Comparability operators

The most typical factor you’ll use if statements in PowerShell are for evaluating two gadgets with one another. Powershell has particular operators for various comparability situations. Whenever you use a comparability operator, the worth on the left-hand facet is in comparison with the worth on the right-hand facet.

The -eq does equality checks between two values to verify they’re equal to one another.

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

On this instance, I’m taking a identified worth of 5 and evaluating it to my $worth to see in the event that they match.

Different operator’s values that can be utilized –

Operator Comparability
-eq equals
-ne not equals
-gt larger than
-ge larger than or equal
-lt lower than
-le lower than or equal
-like string matches wildcard sample
-notlike string doesn’t match wildcard sample
-match string matches regex sample
-notmatch string doesn’t match regex sample
-contains assortment incorporates a vlaue
-notcontains assortment doesn’t comprise a price
-in worth is in a set
-notin worth just isn’t in a set
-is each objects are the identical kind
-isnot the objects will not be the identical kind

How one can Use If Statements in PowerShell to Examine If A File Exists

Now that we’ve got coated how the If assertion works, I want to present you a typical use case I’ve used the If Assertion many instances earlier than.

I typically discover myself creating scripts that I might solely prefer to run if a specific file exists or doesn’t exist.

For instance, that is nice if you wish to run a script if an utility is put in as a result of a sure file will exist on a pc.

The assertion that you should utilize to see if a file exists is the test-path assertion.

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

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

If (Take a look at-Path -Path E:reportsprocesses.txt ) 
Copy-Merchandise -Path E:reportsprocesses.txt -Vacation spot C:studies

On this instance, I’ll examine if “c:reportsReport1.txt” exists and if it exists, I’ll copy the file to “C:studies”. Right here is the script that can do the job.

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

Within the final sub-section, you noticed easy methods to examine if a file exists and duplicate the file. What if you wish to copy the file as an alternative?

If you wish to delete the file as an alternative of copying it, change the Copy-Merchandise command with the Take away-Merchandise command.

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

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

Closing

PowerShell is an especially highly effective device that each sysadmin needs to be utilizing. The if assertion is such a easy assertion however is a really elementary piece of PowerShell, permitting you to automate complicated duties primarily based and conditional decision-making. You’ll find your self utilizing this a number of instances in nearly each script you write. I hope this text has given you a greater understanding than you had earlier than.