The if -not operator works inversely to the functionality of the if conditional statements. This post demonstrates the functionality of PowerShell if -not operator.
How Does the if -not Operator Work in PowerShell?
The if -not operator is the reverse of the if-operator in PowerShell. It means that if the condition is false, then the if -not operator will reverse it to true, and the if-block will be executed.
Let’s start the journey of the if -not operator by understanding its syntax.
Syntax
{statement(s)}
If the condition is false, the -not operator will revert it to true and the statements will be executed that are inside the if -not body.
Let’s understand the concept of the if -not operator in detail.
Example 1: Using if -not Operator on a True Condition
In the first example, the if -not operator is used as the conditional operator to check the comparison of numerical values in PowerShell. The following code is exercised to check the usage of the if -not operator when the condition is true:
if( -not(5 -lt 10))
{
Write-Host "THE EXECUTION OF IF CONDITION IS TRUE"
}
else
{
Write-Host "THE EXECUTION OF ELSE CONDITION IS TRUE"}
In the above code:
- The condition is set to “5 -lt 10” and is being used alongside the -not operator.
- One statement is written in the if as well as in the else block.
The condition “5 -lt 10” itself is true, but the -not will turn it to false.
Output
As the condition turns false, the else statement is executed.
Example 2: Using if -not Operator on a False Condition
In this example, we are considering a condition that is false and will see how the if -not operator behaves in this situation.
if( -not(15 -lt 8))
{
Write-Host "THE EXECUTION OF IF CONDITION IS TRUE"
}
else
{
Write-Host "THE EXECUTION OF ELSE CONDITION IS TRUE"
}
In the above code, the condition is set to “15 -lt 8“, which is false. However, the -not keyword will make it true.
Output
The condition was false, but the if -not operator changed it to true; therefore, the if part of the code is executed.
Example 3: Check the Presence of an Array Element Using the if -not Operator
With the help of the If -not operator, you can check the presence of an array element. Let’s practice this usage of If -not via the below mentioned example:
Code
$array=("henry","devi","john","ali")
$input=Read-Host "Please enter a name"
if(-not($array -contains $input))
{
Write-Host "Entered Name Not Present in the Class"
}
else
{
Write-Host "Entered Name Present in the Class"
}
In the above code:
- An array ($array) of strings is initialized which contains four elements.
- The Read-Host cmdlet is used to take input from the user.
- The if condition is set to ($array -contains $input) and is enclosed using the -not keyword.
- If the conditional statement is true, the -not keyword turns it false and vice versa.
Output
The user inputs a value “ali” and the condition is true as “ali” is in the $array. However, the -not has changed the expression to false, so the else block is executed and printed on the terminal.
That’s it! You have learned the working and usage of an if -not operator in PowerShell.
Conclusion
The If -not operator works reciprocally to the if statement. The -not operator turns the true condition to false and vice versa. Therefore, if the condition is false, the if-block is executed, and the else block is in the case of true conditions. This article demonstrates the working of the If -not operator in PowerShell.