Introduction: Windows PowerShell supports all operators which are supported by any programming or scripting language. Operators are responsible for manipulating the values to get the desired output.
Types of Operators: Powershell supports an exhaustive list of operators and, frequently used operators are listed but are not limited.
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Redirection Operators
- Split Operators
- Join Operators
- Type Operators
- Pipeline Operators
Detailed discussion about these operators are listed below
Arithmetic Operators: Arithmetic operators and Bitwise operators are listed with PowerShell expression and self-explanatory. Bitwise operators only on integer data types.
Operators | Expressions | Output |
+ | 8 + 2 “Power” + “Shell” @(8) + @(8) | 10 PowerShell 8 8 |
– | 8 – 2 -8 | 6 -8 |
* | 8 * 2 @(8) * 8 | 16 8 8 8 8 8 8 8 8 |
/ | 8 / 2 | 4 |
% | 8 % 2 9 % 2 | 0 1 |
-band | 8 -band 2 | 0 |
-bnot | -bnot 8 | -9 |
-bor | 8 -bor 2 | 10 |
-bxor | 8 -bxor 2 | 10 |
-shl | 8 -shl 2 | 32 |
-shr | 8 -shr 2 | 2 |
Assignment Operators: These operators are used to assign the value to a variable. These can manipulate the values before assign to a variable. Examples are shown below.
Operators | Expressions | Output |
+= | $v = 5 $v += 6 $v $str = ”Power” $str += “Shell” $str | 11 PowerShell |
-= | $v = 5 $v -= 6 $v $arr = 5, 6, 7 $arr[2] -= 2 $arr | -1 5 6 5 |
*= | $v = 5 $v *= 6 $v $arr = 5, 6, 7 $arr[2] *= 2 $arr | 30 5 6 14 |
/= | $v = 12 $v /= 6 $v $arr = 5, 6, 7 $arr[1] *= 2 $arr | 2 5 3 7 |
%= | $v = 12 $v %= 7 $v | 5 |
++ | $v1 = 5 ++$v1 $v1 $v2 = 5 $v3 = $v2++ $v3 | 6 5 |
— | $v1 = 5 –$v1 $v1 $v2 = 5 $v3 = $v2– $v3 | 4 5 |
Comparison Operators: These are used to compare the values of variables, determine specified pattern to variable value. These operators are categories in Equality, Matching, Containment, Replacement and Type. These operators are explained in the below tables.
Equality Operators | Expressions | Output |
-eq | 5 -eq 5 | True |
-ne | 4 -ne 5 | True |
-gt | 6 -gt 5 | True |
-ge | 6 -ge 5 | True |
-lt | 5 -lt 6 | True |
-le | 5 -le 6 | True |
Matching Operators | Expressions | Output |
-like | “Operating System” -like “*rating*” | True |
-notlike | “Operating System” -notlike “*Windows*” | True |
-match | “India”, “USA”, “Japan” -match “India” | India |
-notmatch | “India”, “USA”, “Japan” -notmatch “India” | USA Japan |
Containment Operators | Expressions | Output |
-contains | “India”, “USA”, “Japan” -contains “India” “India”, “USA”, “Japan” -contains “India”, “USA” | True False |
-notcontains | “India”, “USA”, “Japan” -notcontains “UK” “India”, “USA”, “Japan” -notcontains “India”, “UK” | True True |
-in | “India” -in “India”, “USA”, “Japan” “South India” -in “India”, “USA”, “Japan” | True False |
-notin | “India” -notin “India”, “USA”, “Japan” “South India” -notin “India”, “USA”, “Japan” | False True |
Replacement Operators | Expressions | Output |
-replace | “Powershell” -replace “s” “S” | PowerShell |
-ireplace | “Powershell” -replace “s” “S” | PowerShell |
-creplace (Case Sensitive) | “Powershell” -replace “S” “s” | PowerShell |
Type Operators | Expressions | Output |
-is | $v1 = 5 $v2 = “5” $v1 -is [Int] $v1 -is $v2.GetType() | True False |
-isnot | $v1 = 5 $v2 = “5” $v1 -isnot [Int] $v1 -isnot $v2.GetType() | False True |
Logical Operators: These operators are used to join with multiple expression and statements which helps to test many conditions at a same time. These operators work with comparison operators to evaluate multiple conditions.
Type Operators | Expressions | Output |
-and | (“PowerShell” -eq “PowerShell”) -and (“Office365” -eq “Office365”) | True |
-or | (“PowerShell” -eq “Power Shell”) -or (“Office365” -eq “Office365”) (“PowerShell” -eq “Power Shell”) -or (“Office365” -eq “Office”) | False |
-xor | (“PowerShell” -eq “PowerShell”) -xor (“Office365” -eq “Office”) (“PowerShell” -eq “PowerShell”) -xor (“Office365” -eq “Office”) | True False |
-not or ! | -not((“PowerShell” -eq “PowerShell”) -and (“Office365” -eq “Office365”)) !((“PowerShell” -eq “PowerShell”) -and (“Office365” -eq “Office365”)) | False False |
Redirection Operators: These operators are capable to create a command output to a text file. PowerShell provides “Out-File”, “Tee-Object” and redirection operators to implement output redirection. The “Out-File” sends the command output to a text file and, “Tee-Object” send the command output to a text file and then sends it to the pipeline.
Redirection operators output stream represent by a number which represent the output stream.
Stream | Stream Type |
1 | Success |
2 | Error |
3 | Warning |
4 | Verbose |
5 | Debug |
6 | Information |
* | All Streams |
Split Operators: The operator splits a long string into substrings based on the provided delimiter. Split operator can be control based on type of delimiter, maximum number of substring and delimiter matching condition to perform split.
Notation | Description | Example (All Operators) |
> | Sends stream to a file | (“PowerShell” -eq “PowerShell”) -and (“Office365” -eq “Office365”) 1> “C:\Temp\RedirectOp.txt” (“PowerShell” -eq “PowerShell”) -xor (“Office365” -eq “Office”) 1>> “C:\Temp\RedirectOp.txt” -not((“PowerShell” -eq “PowerShell”) -and (“Office365” -eq “Office365”)) 1>> “C:\Temp\RedirectOp.txt” !((“PowerShell” -eq “PowerShell”) -and (“Office365” -eq “Office365”)) 2>&1 1>> “C:\Temp\RedirectOp.txt” |
There are many options available with split operator which can be passed while doing splitting. These options can only be used with <Max-substrings>parameter. These options are “SimpleMatch” and “RegexMatch”.
SimpleMatch: The syntax is “SimpleMatch [,IgnoreCase]”. This option cannot be used with RegexMatch. IgnoreCase parameter case-insensitive match even if used with -cSplit operator.
RegexMatch: The syntax is
“[RegexMatch] [,IgnoreCase] [,CultureInvariant] [,IgnorePatternWhitespace] [,ExplicitCapture] [,Singleline | ,Multiline]”.
Let’s follow the examples to understand it practically.
Description | Script | Output |
Default Split with whitespaces | -split “India USA Japan” | India USA Japan |
Split with delimiter comma “,” | “India,USA,Japan” -split ‘,’ | India USA Japan |
Split with string pattern | “Sunday,Monday,Tuesday, Wednesday,Thursday,Friday, Saturday” -split ‘day’ | Sun ,Mon ,Tues ,Wednes ,Thurs ,Fri ,Satur |
Restrict output to a given number | “Sunday,Monday,Tuesday, Wednesday,Thursday,Friday, Saturday” -split ‘day’, 4 | Sun ,Mon ,Tues ,Wednesday,Thursday,Friday,Saturday |
Rule to apply conditional delimiter | $day = Sunday,Monday,Tuesday” $day -split {$_ -eq ‘d’} | Sun ay,Mon ay,Tues ay |
Multiple string spilt | “Sunday,Monday,Tuesday”, “Wednesday,Thursday,Friday,Saturday” -split ‘day’, 4 | Sun ,Mon ,Tues Wednes ,Thurs ,Fri ,Saturday |
SimpleMatch with IgnoreCase and -csplit | “Sunday,MonDay,Tuesday,Wednesday,ThursDay,Friday,Saturday” -csplit ‘day’, 7, ‘simplematch’,’ignorecase’ | Sun ,MonDay,Tues ,Wednes ,ThursDay,Fri ,Satur |
RegexMatch | $a = @’ 1.He lives in India. 2.He lives in USA. 3.He lives in Japan. ‘@ $a -split “^\d.”, 0, “multiline” | He lives in India. He lives in USA. He lives in Japan. |
RegexMatch “.” | $a = @’ .. .. .. ‘@ $a -split “^\.”, 0, “multiline” | . . . |
* In the RegexMatch, “.” is interpreted to match any character except for a newline character that’s why Split statement returns a blank line for every character except newline.
Join Operators: This operator concatenates a group of strings into a single string. This operator is extremely helpful to create a string from a dynamic set of string.
Description | Expressions | Output |
Join with multiple string | -join “Office”, “365”, “2016” | Office 365 2016 |
unary operator precedence | -join (“Office”, “365”, “2016”) | Office3652016 |
Join with multiple string and delimiter | (“Office”, “365”, “2016”) -join ” “ | Office 365 2016 |
Type Operators: These operators return true or false for a .Net framework object. These are three operators: -is, -isnot and -as.
- -is operator returns true when input is .Net Framework type instance else returns false.
- -isnot operator returns true when input is not .Net Framework type instance else returns false.
- -as converts input to not .Net Framework type.
Description | Expressions | Output |
-is | (“India”, “USA”, “JAPAN”) -is [System.Array] (“India”, “USA”, “JAPAN”) -is [System.String] | True False |
-isnot | (“India”, “USA”, “JAPAN”) -isnot [System.Array] (“India”, “USA”, “JAPAN”) -isnot [System.String] | False True |
-as | (“India”, “USA”, “JAPAN”) -as [System.Array] (“India”, “USA”, “JAPAN”) -as [System.String] | India USA JAPAN India USA JAPAN |
Pipeline Operators: This operator executes sequence of commands one after another hence output of the previous command would be an input to the next command.
Description | Expressions | Output |
Display output from the services | Get-Service xbgm | Format-Table -Property name, status | Name Status —- —— xbgm Stopped |
Conclusion: This article covers most of the PowerShell operators without them automation is impossible; therefore, operator types give the understanding of when to use which type of operator.
Leave a Reply