When you are setting up out learning how to generate PowerShell scripts to accomplish tasks for you it is definitely fascinating when you see your script function the way it really should. Now it is time to just take it to the up coming level and give your script the capability to make choices employing conditional logic statements. The PowerShell if statement assemble is a prevalent way to define conditions inside of your script. If statements in PowerShell mimic the conclusion-making course of action persons use every single working day. If a problem is fulfilled, then one thing takes place. For illustration, if it is raining exterior, I’ll get an umbrella before heading outside the house.
In this diagram, if the issue is accurate, then it runs a particular command or assertion. If the affliction is wrong, it moves on to the up coming command or assertion. Here’s a basic PowerShell case in point.
If statements in PowerShell
The syntax of If statements in PowerShell is fairly essential and resembles other coding languages.
if (condition) assertion or command
$issue = $correct
if ( $issue )
Generate-Output "The affliction was accurate"
The very first thing the
if statement does is evaluate the expression in parentheses. If it evaluates to
$correct, then it will execute the
scriptblock in the braces. If the price was
$phony, then it would skip more than that scriptblock.
The most typical detail you will use
if statements in PowerShell are for comparing two things with just about every other. Powershell has specific operators for diverse comparison situations. When you use a comparison operator, the benefit on the still left-hand aspect is in contrast to the value on the ideal-hand facet.
-eq does equality checks involving two values to make guaranteed they are equal to each other.
$benefit = Get-MysteryValue
if ( 5 -eq $worth )
# do a thing
In this instance, I am using a known benefit of
5 and evaluating it to my
$value to see if they match.
Other operator’s values that can be utilised –
|greater than or equivalent
|considerably less than
|less than or equivalent
|string matches wildcard pattern
|string does not match wildcard pattern
|string matches regex sample
|string does not match regex pattern
|collection incorporates a vlaue
|assortment does not contain a benefit
|value is in a assortment
|price is not in a selection
|both equally objects are the very same style
|the objects are not the same style
How to Use If Statements in PowerShell to Check If A File Exists
Now that we have covered how the If statement will work, I would like to demonstrate you a popular use circumstance I have used the If Assertion several periods prior to.
I often uncover myself generating scripts that I would only like to operate if a certain file exists or does not exist.
For illustration, this is great if you want to operate a script if an application is set up due to the fact a certain file will exist on a pc.
The statement that you can use to see if a file exists is the take a look at-path statement.
Examination-Route -Path c:reportsReport1.txt
If the file exists the Output “True” will be shown
If (Take a look at-Path -Route E:reportsprocesses.txt ) Duplicate-Item -Path E:reportsprocesses.txt -Vacation spot C:experiences
In this illustration, I will check out if “c:reportsReport1.txt” exists and if it exists, I will copy the file to “C:reports”. Below is the script that will do the work.
How To UseIf Statements in PowerShell To Examine If A File Exists And Delete It
In the previous sub-section, you observed how to check out if a file exists and duplicate the file. What if you want to duplicate the file alternatively?
If you want to delete the file alternatively of copying it, substitute the Copy-Product command with the Eliminate-Product command.
Below is the current script that makes use of PowerShell “IF” statement to check if a file exists. Then, if it exists, delete it…
$fileexists = Check-Path -Path E:reportsfirst-file.txt If ($fileexists ) Take out-Product -Path E:reportsfirst-file.txt -Force
PowerShell is an really highly effective instrument that each and every sysadmin should really be employing. The
if statement is such a straightforward assertion but is a incredibly elementary piece of PowerShell, letting you to automate sophisticated jobs primarily based and conditional final decision-building. You will locate yourself making use of this many situations in practically every script you create. I hope this posting has provided you a far better knowledge than you had just before.