if-else Statements in PowerShell
[ad_1]
This write-up will current a radical information for the if-else statements:
- What’s if-statement in PowerShell?
- Syntax of if-statement.
- What’s if-else assertion in PowerShell?
- Syntax of if-else assertion.
So let’s get began!
What’s if-statement in PowerShell?
The if assertion in PowerShell takes an expression/situation in its parenthesis and exams it. Consequently, it is going to return both a real or false worth, if the required situation is true then the code-block related to the if-statement will get executed. The if-statement offers with the true situation solely, it has nothing to do with the false situation.
Syntax of if-statement
The below-given snippet exhibits the fundamental syntax of if-statement in PowerShell:
if(expression/situation) {
// Executes solely if the given expression is true
}
Let’s think about the under script to know the working of if-statement in PowerShell:
$a =12;
$b =15;
if($a -le $b) {
write-host(“a is lower than or equal to b”);
}
On this instance program, we utilized the if-statement to check an expression, if the returned worth is true then the physique of if-statement will execute else not:
The output verified the working of the if-statement.
What if the returned worth of the required expression is fake? How if-statement will take care of the false worth?
$a =12;
$b =15;
if($a -ge $b) {
write-host(“a is lower than or equal to b”);
}
The above script will generate the next output:
The cursor moved to the following line with out performing any particular process. It verified that the if-statement doesn’t deal with the false circumstances.
What’s if-else assertion in PowerShell?
To sort out the false circumstances, the else assertion can be utilized together with the if-statement. In Powershell, if we utilized the mixture of if and else statements, because of this, each true and false circumstances shall be tackled.
Syntax of if-else assertion
The under snippet depicts the fundamental syntax of the if-else assertion in PowerShell:
if(test-condition/expression) {
// Executes solely if the given expression is true
}
else{
// Executes if the required expression will not be true
}
use if-else assertion in PowerShell
Under snippet will help you on this regard:
$a =20;
$b =15;
if($a -le $b) {
write-host(“a is lower than or equal to b”);
}
else{
write-host(“a is larger than b”);
}
This time we utilized each if and else statements, now if the worth of a is lower than or equal to the b then physique of if-statement will execute in any other case the physique of else-statement will execute:
The above snippet verified that the else-statement was executed as a result of the required situation was false.
Conclusion
In PowerShell, decision-making statements equivalent to if, else, and else-if are used to handle this system’s circulate primarily based on totally different circumstances. The if-statement offers with the true situation solely whereas the else-statement offers with the false situation solely. Subsequently, in PowerShell, if and else statements can be utilized combinedly to deal with each true and false circumstances. This write-up defined all of the fundamentals of if and else circumstances in PowerShell utilizing some appropriate examples.
[ad_2]
Supply hyperlink