Professional Documents
Culture Documents
PDF 24
PDF 24
$eggs = 10
if ($eggs -lt 12) {
"You have less than a dozen eggs."
}
Skip to content
In this example, we created a variable called $eggs and gave it a value
of 10. Next, we set a conditional statement that says if $eggs is less
than 12, display a message. Since $eggs has a value of 10, the message
"You have less than a dozen eggs" is displayed.
Now that we have a basic understanding of If statements, let's dive a
little deeper and go over the syntax and some more advanced
examples.
PowerShell If statement syntax
The syntax of If statements in PowerShell is pretty basic and resembles
other coding languages.
if (condition)
{statement or command}
-eq equals
if (Test-Path 'c:\temp\macgyver_biography.txt') {
Get-Content 'c:\temp\macgyver_biography.txt' | Measure-Object -Word
}
Skip to content
In this example, we are calling the Test-Path cmdlet to see if a file
exists or not. If the file exists, we use the Get-Content and Measure-
Object cmdlets to return a word count of the file. If the file does not
exist, then the script will just end. As you can see from the screenshot,
my MacGyver biography is only 8 words long so far. One of these days,
I'll finish it.
PowerShell If-Else statements
Up to this point, we've only talked about If statements. However, you'll
often find If statements accompanied by an Else statement. Else
statements allow you to perform an additional action if the condition is
not met or returns false.
Skip to content
In this diagram, you can see that we now have two statements that can
be executed. One statement if the condition returns true, and one
statement if the condition returns false. Here's a simple PowerShell If-
Else statement example.
$x = 4
if ($x -ge 3) {
"$x is greater than or equal to 3"
}
else {
"$x is less than 3"
}
Now that the condition returns false, you can see that PowerShell is
returning our Else statement, "$x is less than 3."
Nesting conditional statements
PowerShell allows you to nest If and Else statements within If and Else
statements (incoming Inception vibes). Nested conditional statements
basically cycle through the statement until either a statement is
returned true or until all statements are returned false. There are a
couple of different ways to nest conditional statements. One way is to
literally add a new If-Else statement inside an If or Else script block. For
example.
Skip to content
if (condition1) {
"condition1 is true"
}
else {
if (condition 2) {
"condition 2 is true"
}
else {
"condition 2 is false"
}
}
$eggs = 14
if ($eggs -eq 12) {
"You have exactly a dozen eggs."
}
elseif ($eggs -lt 12) {
"You have less than a dozen eggs."
}
else {
"You have more than a dozen eggs."
}
Skip to content
In this example, we have three possible outcomes. One if we have
exactly 12 eggs. One if we have less than 12 eggs. And one if we have
more than 12 eggs. In the screenshot above, we have our $egg variable
set to 14, which returned the Else statement, displaying the message
"You have more than a dozen eggs."
Negating PowerShell statements
Sometimes with operators, we need to negate the statement. Using the
previous MacGyver example, what if instead of searching for the
MacGyver biography, we want to make sure it isn't there? Here's an
example of how to do that:
Skip to content
PowerShell dinner menu
Now that we know all about If statements, Else statements, and nested
conditional statements, let's bring it all together by creating a script
that will give us our dinner plans depending on what day of the week it
is.
First, we'll get the day of the week using the Get-Date cmdlet, returning
the DayOfWeek property and assigning it to the $day variable.
$day = (Get-Date).DayOfWeek
Next, we'll build our nested conditional statement for the different days
of the week and assign a different meal for each day.
Since I ran this command on a Thursday, the returned dinner plan was
"Tilapia Thursday." While this script runs as planned and returns the
correct results, I need to add a caveat. If you are nesting multiple
conditional statements together, you should be using the Switch
statement instead. The Switch statement will usually run faster and look
cleaner, making it easier to understand compared to multiple nested If-
Else statements. If you want to learn more about the Switch statement,
Skipintoa content
stay tuned as we'll cover it more in-depth future article.
Wrapping up
PowerShell is an extremely powerful tool that every sysadmin should be
using. It becomes even more powerful when you start taking advantage
of If-Else statements, allowing you to automate complex tasks based
and conditional decision making. Be sure to check out how PowerShell
can also help you secure your passwords and schedule tasks.
If you're interested in other powerful tools, make sure you download a
free trial of PDQ Deploy and PDQ Inventory. PDQ Deploy will help you
keep your network environment up to date, making deployments and
updates a breeze. PDQ Inventory will ensure you have all the
information you need to properly manage all of your Windows devices.
PDQ Inventory also gives you access to the PowerShell scanner, letting
you take advantage of all the cool PowerShell knowledge you just
learned in this article.
Brock Bingham
Born in the '80s and raised by his NES, Brock quickly fell in
love with everything tech. With over 15 years of IT
experience, Brock now enjoys the life of luxury as a
renowned tech blogger and receiver of many Dundie
Awards. In his free time, Brock enjoys adventuring with his
wife, kids, and dogs, while dreaming of retirement.
Related articles
Skip to content
Skip to content
Skip to content
GENERAL IT
SaaS user management best practices
Skip to content
POWERSHELL
How to send a Wake-on-LAN (WOL) magic
packet with PowerShell
Skip to content
Products
PDQ Connect
PDQ Detect
PDQ Deploy
PDQ Inventory
Product comparison
SimpleMDM
SmartDeploy
Downloads
Security
Status
PRODUCT
Solutions
Inside look: How PDQ's IT team uses PDQ
Detect
Desktop management
IT asset management
Patch management
Remote device management
Software inventory management
Software deployment
Company
About
Careers
Contact
EULA
Privacy Policy
Additional Policies
Terms of Use
Tax documents Skip to content
Sitemap