System administrators make decisions daily regarding what maintenance tasks to perform on servers based on numerous criteria. Automating repetitive administrative tasks with PowerShell frequently involves using logic to replicate this decision making process. Several techniques can be used in order to achieve the desired results using comparisons, filters, and conditional logic.
You won't get far in creating PowerShell scripts without performing conditional logic, which begins with comparing values. The ability to check if a user exists, if a file has been created, or if one computer is able to connect to another all require a comparison to a value. The one big gotcha in PowerShell has to do with syntax: rather than using traditional comparison operators like < or > PowerShell uses -lt or -gt to perform comparisons.
Several comparison operators are most commonly used with numerical values, although they have their place when working with dates or version numbers and other variable types as well. The following table contains the comparison operators most commonly used to compare numbers.
| Purpose | Operator | Example |
|---|---|---|
| Greater than | -gt | 1 -gt 2 (Returns $false) |
| Less than | -lt | 1 -lt 2 (Returns $true) |
| Equals | -eq | 2 -eq 1+1 (Returns $true) |
| Not equals | -ne | 3 -ne 1+1 (Returns $true) |
| Greater than or equal to | -ge | 3 -ge 1+1 (Returns $true) |
| Less than or equal to | -le | 2 -le 1+1 (Returns $true) |
When comparing against text strings -eq can be used when an exact match is required. The -match operator can be used when looking for a portion of a string, or -like can be used to perform wildcard searches. PowerShell can also be used to search for a particular value within an array by using -in, -notin, -contains, or -notcontains.
The if statement runs code blocks based on the results of one or more conditional tests
$input = Read-Host 'Is it Morning (M), Afternoon (A), or Evening (E)? ' if($input -eq 'm') { Write-Output 'Good morning!' } elseif($input -eq 'a') { Write-Output 'Good afternoon!' } elseif($input -eq 'e') { Write-Output 'Good evening!' } else { Write-Output 'Hello!' }
The switch statement checks multiple conditions for a given value and runs the corresponding code blocks.
$input = Read-Host 'Is it Morning (M), Afternoon (A), or Evening (E)? ' switch($input) { m {'Good morning!'} a {'Good afternoon!'} e {'Good evening!'} default {'Hello!'} }
The -match operator is used to match regular expressions.
$input = Read-Host 'Enter zip code' if(!($input -match '^\d{5}(-\d{4})?$')) { Write-Output "$input is not a valid zip code." }
The switch -Regex statement checks multiple conditions for a given value based on regular expression matching and runs the corresponding code blocks.
$input = Read-Host 'Enter something' switch -Regex ($input) { '.' {'You entered a character'} '\d' {'You entered a digit'} '\s' {'You entered a space'} '\w' {'You entered a word'} '\w \w' {'You entered multiple words'} default {'You entered nothing'} }
The break statement immediately exits the nearest enclosing Foreach, For, While, Do, or Switch statement.
$input = Read-Host 'Enter something' switch -Regex ($input) { '^.$' { 'You entered a character' break } '^\d$' { 'You entered a digit' break } '^\s$' { 'You entered a space' break } '\w \w' { 'You entered multiple words' break } '\w' { 'You entered a word' break } default { 'You entered nothing' } }
In cases where more than one condition must be met, parenthetical statements can be used in order to manage groups of conditions. The following example can be used to select processes related to various web browsers:
PS> Get-Process | Where-Object {($_.Name -eq "iexplore") -or ($_.Name -eq "chrome") -or ($_.Name -eq "firefox")}
PowerShell 4 allows you to use Where-Object using aliases such as ? or Where, and accepts shortcut notation for the condition as well
PS> Get-Process | ? {($_.Name -eq "iexplore") -or ($_.Name -eq "chrome") -or ($_.Name -eq "firefox")}