How to Use If Statements in PowerShell –
4 min read
When you are starting up out discovering how to publish PowerShell scripts to complete jobs for you it is genuinely fascinating when you see your script function the way it really should. Now it is time to consider it to the subsequent stage and give your script the skill to make selections working with conditional logic statements. The PowerShell if assertion construct is a widespread way to outline ailments within just your script. If statements in PowerShell mimic the determination-earning procedure folks use every single working day. If a affliction is met, then some thing happens. For example, if it is raining outdoors, I’ll seize an umbrella right before heading exterior.

In this diagram, if the situation is accurate, then it runs a certain command or assertion. If the ailment is phony, it moves on to the next command or assertion. Here’s a simple PowerShell case in point.
If statements in PowerShell
The syntax of If statements in PowerShell is fairly simple and resembles other coding languages.
if (situation) statement or command
or
$affliction = $genuine
if ( $issue )
Write-Output "The issue was accurate"
The very first detail the if
statement does is evaluate the expression in parentheses. If it evaluates to $genuine
, then it will execute the scriptblock
in the braces. If the price was $false
, then it would skip around that scriptblock.
Comparison operators
The most common thing you will use if
statements in PowerShell are for comparing two merchandise with each other. Powershell has distinctive operators for unique comparison situations. When you use a comparison operator, the benefit on the left-hand side is in comparison to the value on the proper-hand side.
The -eq
does equality checks among two values to make sure they are equal to just about every other.
$value = Get-MysteryValue
if ( 5 -eq $worth )
# do something
In this illustration, I am using a recognised worth of 5
and evaluating it to my $value
to see if they match.
Other operator’s values that can be applied –
Operator | Comparison |
---|---|
-eq | equals |
-ne | not equals |
-gt | higher than |
-ge | larger than or equal |
-lt | significantly less than |
-le | less 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 pattern |
-contains | collection is made up of a vlaue |
-notcontains | selection does not contain a worth |
-in | value is in a assortment |
-notin | price is not in a collection |
-is | both equally objects are the same sort |
-isnot | the objects are not the identical kind |
How to Use If Statements in PowerShell to Check If A File Exists
Now that we have included how the If assertion performs, I would like to show you a prevalent use scenario I have employed the If Assertion several periods just before.
I generally locate myself generating scripts that I would only like to operate if a particular file exists or does not exist.
For instance, this is good if you want to run a script if an application is mounted simply because a particular file will exist on a laptop or computer.
The statement that you can use to see if a file exists is the take a look at-route statement.
Take a look at-Route -Route c:reportsReport1.txt
If the file exists the Output “True” will be shown
If (Take a look at-Route -Path E:reportsprocesses.txt ) Duplicate-Merchandise -Path E:reportsprocesses.txt -Spot C:stories
In this case in point, I will examine if “c:reportsReport1.txt” exists and if it exists, I will copy 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 past sub-section, you observed how to check if a file exists and copy the file. What if you want to duplicate the file as an alternative?
If you want to delete the file instead of copying it, swap the Duplicate-Merchandise command with the Remove-Item command.
Here is the current script that makes use of PowerShell “IF” statement to check if a file exists. Then, if it exists, delete it…
$fileexists = Test-Path -Path E:reportsfirst-file.txt If ($fileexists ) Remove-Item -Route E:reportsfirst-file.txt -Pressure
Closing
PowerShell is an exceptionally impressive instrument that every single sysadmin really should be applying. The if
statement is this kind of a simple assertion but is a really elementary piece of PowerShell, allowing for you to automate advanced duties based mostly and conditional selection-making. You will find yourself employing this many occasions in just about every single script you create. I hope this short article has given you a improved knowing than you had right before.