You are currently viewing Conditions In PowerShell

Conditions In PowerShell

Conditions In PowerShell

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed operators in PowerShell.

https://cloudaffaire.com/operators-in-powershell/

In this blog post, we will discuss conditions in PowerShell. Like any other scripting language, PowerShell also provides conditions. You can use conditions in PowerShell script for decision making. In PowerShell we mainly use IF conditional statement for decision making. PowerShell supports if, if else and if elseif else conditional statements. Apart from if, switch can also be used as conditional statement.

Conditions In PowerShell:

If, if-else and if-elseif-else:

if, if else and if elseif else condition block can be used for decision making in PowerShell. If, if else and if elseif else condition block also supports nesting.

  • if: if is the simplest conditional block that you can define in a PowerShell script. If evaluates the condition and if the condition is true subsequent command gets executed.
  • if else: Unlike if, if else evaluates both true and false outcomes of the given condition.
  • If elseif else: Using if elseif else, you can evaluate multiple conditions in your shell script. Multiple elseif blocks can be defined in if elseif else block depending upon your requirement.

Switch:

Switch statements can be used to evaluate multiple conditions at once. You can define an action for each condition in switch statement and if the condition evaluates to “True” the action is performed. There is also a default action block that gets executed if no other condition is satisfied.

Switch statement also supports different parameters that can be used to control the switch behavior. Switch has the following parameters:

  • Wildcard: Indicates that the condition is a wildcard string. If the match clause is not a string, the parameter is ignored. The comparison is case-insensitive.
  • Exact: Indicates that the match clause, if it is a string, must match exactly. If the match clause is not a string, this parameter is ignored. The comparison is case-insensitive.
  • CaseSensitive: Performs a case-sensitive match. If the match clause is not a string, this parameter is ignored.
  • File: Takes input from a file rather than a value statement. If multiple File parameters are included, only the last one is used. Each line of the file is read and evaluated by the Switch statement. The comparison is case-insensitive.
  • Regex: Performs regular expression matching of the value to the condition. If the match clause is not a string, this parameter is ignored. The comparison is case-insensitive. The $matches automatic variable is available for use within the matching statement block.

Hope you have enjoyed this article. In the next blog post, we will discuss Loops in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1