Powershell Logic
The If Condition
Powershell lets you execute code based on conditions.
To test a condition you use an if statement. The if statement returns true or false, based on your test:
The if statement starts a script block
The condition is written inside parenthesis
The code inside the braces is executed if the test is true
Operator and its Description
| 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 |
Example
$a = "The quick brown fox jumped over the lazy dog"
$z = "fox"
If ($a -contains $z)
{
$b = "The line is " + $True
}
Else
{
$b = "This is lie and " + $false
}
$b
Copy and Try it
Outpout will be else block
Example 2
Example
$Users = "John Doe","Dave Davis","Dick Jones"
$Users -contains "John Doe"
The result is True
Copy and Try it
Examples for comparison operators are -eq, -ne, -ge, -notlike, -gt, -lt, -le, -like, -match, -notmatch, -contains, -notcontains, -is, -isnot, -in, -notin, and so on.
More details Powershell Operators
More details on Array-Hashes