Powershell

PowerShell If statement

PowerShell has a scripting language nature and thus supports the basic fundamentals of any scripting language. If the statement is a conditional programming statement that is used to make decisions in programs based on conditions. If the condition is true then it will execute the program, if it is false then it will terminate the program. These statements assist in creating the logic for programs being written in PowerShell. This post demonstrates the working and functionality of the if-statement in PowerShell.

How to use if-statement in PowerShell

If statements are the conditional blocks that execute the specific program. If the certain condition is true. The If statements in PowerShell perform the functionality similar to the functionality of if statements in other programming languages. conditional statements.

The if-statements are used to perform the logical operations, based on logical reasoning. The possible syntax of the If-Statement in PowerShell is written below.

Syntax

If (condition)
   {statement(s)}

The syntax instances are described as,
If: It is a default keyword used for If statements

(condition): In this parameter, you put or write a specific condition

Statement(s): It contains an executable code, which will be executed only when the condition is true.

Example 1: How to compare two values in PowerShell?

If you want to compare the variable’s value with any constant number (or any two values), the if-statement can be the best choice in the PowerShell ISE. The following script code initializes a variable named “banana” and the condition is set to be “$banana -lt 10”. If the $banana has a value less than 10(as in our case) then it would execute the body of the if-statement.

$banana=8
if($banana -lt 10){
    "you have less than 10 bananas"
    }

Hence, when the script executed it gave us the output “you have less than 10 bananas” because the condition is true.

Example 2: How to check for even/odd numbers in PowerShell?

As in the other scripting languages, PowerShell’s If-statement can also be used to check if the number is an even or odd. For this purpose, the If-statement is being used in this example.

The following PowerShell script takes the value from the user as an input. After that, the condition ($num%2 = 0) of the If-Statement is set on that variable. If the condition is satisfied, the number will be even else the number will be odd.

$num = Read-Host "Enter the number to check for even or odd!"
if($num%2 -eq 0){
    Write-Host "$num is an even number"
    break
    }
    Write-Host "$num is an odd number"

Let’s execute the script and provide an even number as we did here:

It is observed from the output that the number 10 is provided as an input and the output is printed that the number is even.

Similarly, we have again run the script and now an odd number is provided as an output.

The script’s output shows that, when the odd value is provided, the program finds the condition ($num % 2 -eq 0) is false and prints that the inserted number is odd.

Example 3: Nested-if statements to compare multiple values in PowerShell

The nested statements are used to test a condition within another condition. The nested if is quite helpful if you want to put multiple filters to get the output. As an example code, the following script is created which exercises the two if-statements.

The working of the script is provided as follows:

  • Three numbers are taken from the user
  • first, if-statement compares the second with a first number
  • if the second number is greater than the first then the second number will be compared with the third
  • the second if-statement checks if the second number is less than the third number or not
$a = Read-Host "Enter first number"
$b = Read-Host "Enter second number"
$c = Read-Host "Enter third number"
if($b -gt $a){
    if ($b -lt $c)
    {
    Write-Host "$c is greater than both $a and $b"
    }
    Write-Host "$b is greater than $a but less than $c"
    }
    Write-Host "$a is greater than $b"

Let’s execute the script:

Three numbers are taken as an input from the user. The condition of the first if-statement therefore nested-if statement is bypassed and the code written outside the body of the first if-statement is executed.
Here you go! You can now apply PowerShell if-statements in various scenarios of programming.

Note: Click here to learn various methods to run a PowerShell script.

Conclusion

PowerShell if statement is a conditional logic statement that is used to copy the human thinking and decision-making process. In the if statement when the condition is met or proved to be true then it will execute the program and if the condition is false it will not execute the program. PowerShell processes the if-statements like any other advanced programming language. This post has demonstrated the working as well as the functionality of the if-statements in PowerShell.

About the author

Adnan Shabbir