• Site for Newbie Windows Administrators
admincentric
  • Home
  • Active Directory
  • Hyper-V
  • PowerShell
  • Free Tools
  • Contact

PowerShell If Statement

Feb 18, 2024 Admin

In PowerShell, the if statement is used for conditional branching, allowing you to execute different blocks of code based on whether a specified condition is true or false. The basic syntax of the if statement in PowerShell is as follows:

Syntax

if (condition)
{
# Code to execute if the condition is true
}

Syntax Explanation

Here's a breakdown of the components:

  • if: This keyword initiates the conditional statement.
  • (condition): This is the condition that is evaluated. If the condition is true, the code block following it will be executed. If false, the code block is skipped.
  • {}: These curly braces contain the code block to be executed if the condition is true. It's important to note that the braces are mandatory even if there's only one statement to be executed.

Example 1

Here's an example:

$number = 10

if ($number -gt 5)
{
Write-Host "The number is greater than 5."
}

In this example, if the value of the variable $number is greater than 5, the message "The number is greater than 5." will be displayed.

Example 2

You can also use an else block to specify code that will be executed when the condition is false:

$number = 3

if ($number -gt 5)
{
Write-Host "The number is greater than 5."
}
else
{
Write-Host "The number is not greater than 5."
}

In this case, if the value of $number is not greater than 5, the message "The number is not greater than 5." will be displayed.

Example 3

Additionally, you can use elseif to test multiple conditions:

$number = 5

if ($number -gt 5)
{
Write-Host "The number is greater than 5."
}
elseif ($number -eq 5)
{
Write-Host "The number is equal to 5."
}
else
{
Write-Host "The number is less than 5."
}

This example checks if $number is greater than 5, equal to 5, or less than 5 and prints the corresponding message.

Home

Active Directory

Hyper-V

PowerShell

Free Tools

Contact

Trending News

How to enable Hyper-V in Windows? Coming Soon...

How to configure a Virtual Machine? Coming Soon...

Using PowerShell to manage a Virtual Machine Coming Soon...

How to install Active Directory on Windows Server 2016? Coming Soon...

How to create Active Directory Users? Coming Soon....

How to reset Active Directory User Passwords? Coming Soon...

How to unlock Active Directory User Accounts? Coming Soon...

Popular Resources

Popular resource 1 coming soon....coming soon

Popular resource 1 coming soon....coming soon

Popular resource 1 coming soon....coming soon

Popular resource 1 coming soon....coming soon

Table of Contents

Table of Contents

  • Syntax
  • Syntax Explanation
  • Example 1
  • Example 2
  • Example 3

Connect with us!

Company
  • About admincentric
  • Advertise with us
  • Contact
Pages
  • Webinars
  • Deals Store
  • Privacy Policy
Deals
  • Hacking
  • Development
  • Android
RSS Feeds Contact Us

© admincentric, 2023. All Rights Reserved.