Logical Operators in Conditions | Used to combine multiple conditions within an if or switch statement. | if (<condition1> -and <condition2>) { <code> } | powershell<br>$a = 10<br>$b = 20<br>if ($a -gt 5 -and $b -gt 15) {<br> Write-Output "Both are greater"<br>}<br> |
Using -not Operator | Negates the result of a condition. | if (-not <condition>) { <code> } | powershell<br>$a = 5<br>if (-not ($a -gt 10)) {<br> Write-Output "a is not greater than 10"<br>}<br> |
Using -or Operator | Executes the block if either of the conditions is true. | if (<condition1> -or <condition2>) { <code> } | powershell<br>$a = 3<br>$b = 20<br>if ($a -gt 5 -or $b -gt 15) {<br> Write-Output "At least one is greater"<br>}<br> |
Nested If Statements | Places one if statement inside another to handle complex conditional logic. | if (<condition1>) { if (<condition2>) { <code> } } | powershell<br>$a = 15<br>if ($a -gt 10) {<br> if ($a -lt 20) {<br> Write-Output "a is between 10 and 20"<br> }<br>}<br> |
Using -eq , -ne , -lt , -le , -gt , -ge | Common comparison operators to check equality, inequality, and greater/lesser conditions. | if (<value1> -eq <value2>) { <code> } | powershell<br>$a = 10<br>if ($a -eq 10) {<br> Write-Output "a equals 10"<br>}<br> |
Null Checks | Checks if a variable or expression is null before performing operations. | if ($variable -eq $null) { <code> } | powershell<br>$a = $null<br>if ($a -eq $null) {<br> Write-Output "a is null"<br>}<br> |
Conditional Operator (? : ) | PowerShell doesn’t have a built-in ternary operator, but similar behavior can be mimicked with if-else | $result = if (<condition>) { <true-value> } else { <false-value> } | powershell<br>$a = 5<br>$result = if ($a -gt 10) { "Greater" } else { "Lesser" }<br>Write-Output $result<br> |
Type Comparison with -is Operator | Checks whether a value is of a specific type. | if (<value> -is [type]) { <code> } | powershell<br>$a = "Hello"<br>if ($a -is [string]) {<br> Write-Output "a is a string"<br>}<br> |
Existence Check with Test-Path | Verifies if a file or directory exists. | if (Test-Path <path>) { <code> } | powershell<br>$path = "C:\\example.txt"<br>if (Test-Path $path) {<br> Write-Output "File exists"<br>}<br> |
Using Contains Operator | Checks if a collection contains a specific value. | if (<collection> -contains <value>) { <code> } | powershell<br>$array = @(1, 2, 3)<br>if ($array -contains 2) {<br> Write-Output "Array contains 2"<br>}<br> |
Using In Operator | Checks if a value exists within a set of possible values. | if (<value> -in <collection>) { <code> } | powershell<br>$value = 5<br>if ($value -in @(1, 5, 10)) {<br> Write-Output "Value is in the list"<br>}<br> |
Regular Expression Matching with -match | Checks if a string matches a regular expression pattern. | if (<string> -match <regex>) { <code> } | powershell<br>$string = "abc123"<br>if ($string -match '\\d{3}') {<br> Write-Output "String contains three digits"<br>}<br> |