June 21, 2024

charmnailspa

Technological development

How to Use If Statements in PowerShell –

[ad_1]

When you are starting out studying how to produce PowerShell scripts to perform duties for you it is really fascinating when you see your script do the job the way it should really. Now it is time to just take it to the following level and give your script the capacity to make conclusions employing conditional logic statements. The PowerShell if statement construct is a common way to determine conditions within just your script. If statements in PowerShell mimic the final decision-creating procedure men and women use each and every day. If a affliction is satisfied, then a little something takes place. For instance, if it’s raining outside the house, I’ll grab an umbrella right before heading outdoors.

powershell if statements 1

In this diagram, if the situation is true, then it operates a certain command or statement. If the ailment is untrue, it moves on to the next command or assertion. Here’s a uncomplicated PowerShell example.

If statements in PowerShell

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

if (problem) assertion or command

or

    $problem = $true
    if ( $ailment )
    
        Publish-Output "The ailment was real"
    

The very first factor the if statement does is examine the expression in parentheses. If it evaluates to $accurate, then it will execute the scriptblock in the braces. If the benefit was $phony, then it would skip in excess of that scriptblock.

Comparison operators

The most typical issue you will use if statements in PowerShell are for comparing two objects with each other. Powershell has exclusive operators for distinct comparison eventualities. When you use a comparison operator, the value on the still left-hand aspect is compared to the worth on the suitable-hand facet.

The -eq does equality checks between two values to make guaranteed they are equivalent to each individual other.

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

In this case in point, I am using a recognized value of 5 and evaluating it to my $price to see if they match.

Other operator’s values that can be made use of –

Operator Comparison
-eq equals
-ne not equals
-gt greater than
-ge larger than or equivalent
-lt a lot less than
-le much less than or equal
-like string matches wildcard pattern
-notlike string does not match wildcard sample
-match string matches regex sample
-notmatch string does not match regex sample
-contains collection incorporates a vlaue
-notcontains collection does not incorporate a price
-in value is in a selection
-notin value is not in a assortment
-is equally objects are the same type
-isnot the objects are not the exact same form

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

Now that we have lined how the If statement operates, I would like to demonstrate you a typical use case I have used the If Statement quite a few times in advance of.

I usually find myself developing scripts that I would only like to run if a particular file exists or does not exist.

For example, this is terrific if you want to operate a script if an application is put in since a specified file will exist on a personal computer.

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

Check-Route -Path c:reportsReport1.txt

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

If (Test-Path -Route E:reportsprocesses.txt ) 
Duplicate-Product -Path E:reportsprocesses.txt -Spot C:studies

In this instance, I will examine if “c:reportsReport1.txt” exists and if it exists, I will copy the file to “C:reports”. Listed here is the script that will do the position.

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

In the very last sub-part, you noticed how to test if a file exists and copy the file. What if you want to duplicate the file instead?

If you want to delete the file alternatively of copying it, swap the Copy-Merchandise command with the Eliminate-Product command.

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

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

Closing

PowerShell is an exceptionally highly effective device that each and every sysadmin ought to be making use of. The if statement is these a uncomplicated statement but is a very elementary piece of PowerShell, permitting you to automate advanced responsibilities centered and conditional final decision-building. You will find your self utilizing this a number of situations in just about each individual script you produce. I hope this write-up has offered you a greater knowledge than you had in advance of.

[ad_2]

Resource connection