PS
Powershell Tutorial Online

Powershell Conditions


Similar to programming language, in powershell, if condition will check the given condition and if it is true,
it will execute the statements within the first set of braces(from '{' to the next '}').
If the condition is not true, it will check the next condition and so on.
If no given condition is true, the 'else' statements are executed.


Here is the list of all comparing operator with examples.


Operator Description
-eq Equal to
-lt Less than
-gt Greater than
-ge Greater than or Eqaul to
-le Less than or equal to
-ne Not equal to

When comparing text strings, by default PowerShell is not case-sensitive.
But here we have another option to force case-sensitivity.
By appending an "i" to the any operator, you can force PowerShell to be "case-insensitive."
Appending a "c" you can force PowerShell to compare as "case-sensitive."



Use of if else. Only if statement is required, everything else is optional. Powershell doent care of spaces, position of "{" and "}" and formatting. There is not limitation on elseif statements.

As with other programming languages, PowerShell is a dynamic scripting language. Script code can be executed based on conditions that exist or occur. For example, a script might prompt for user input and run a block of code based on the data supplied by the user. A process or application may stop running, triggering an action within a script. Conditional Logic allows us to write scripts in a complex environment, it adds the intelligence required to create decision-making scripts.

Launch PowerShell and let's get started...

Comparing Data

We are going to compare data using the following PowerShell Comparison and Logical Operators.

Table 1: PowerShell Comparison Operators:

Operator

Description

-eq

Equal to

-lt

Less than

-gt

Greater than

-ge

Greater than or Eqaul to

-le

Less than or equal to

-ne

Not equal to

I'm not going to join in the great debate over why these operators where chosen, they exists within PowerShell and must be used. Special note: When comparing text strings, by default PowerShell is not case-sensitive. "TOM" and "tom" would compare "equal to" (-eq). However, you can force PowerShell to compare values based on case-sensitivity. By appending an "i" to the operator, you're telling PowerShell to be "case-insensitive." Appending a "c" forces PowerShell to compare as "case-sensitive."

Examples:
Tom -eq TOM. The result is "True"
Tom -ieq TOM. The result is "True"
Tom -ceq TOM. The result is "False"

Table 2: PowerShell Logical Operators:

Operator

Description

-not

Not

!

Not

-and

And

-or

Or

Now that we have the table listings of Operators, let's do some examples to show their use and results.

7 -eq 7<enter>

Result is "True"

7 -ne 7<enter>

Result is "False"

7 -lt 10<enter>

Result is "True"

7 -gt 10<enter>

Result is "False"

"Tom" -eq "tOm"<enter>

Result is "True"

"Tom" -ceq "tOm"<enter>

Result is "False"