Operators in PowerShell

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.

+8 + 2 “Power” + “Shell” @(8) + @(8)10 PowerShell 8 8
8 – 2 -86 -8
*8 * 2 @(8) * 816 8 8 8 8 8 8 8 8
/8 / 24
%8 % 2 9 % 20 1
-band8 -band 20
-bnot-bnot 8-9
-bor8 -bor 210
-bxor8 -bxor 210
-shl8 -shl 232
-shr8 -shr 22

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.

  +=$v = 5 $v += 6 $v $str = ”Power” $str += “Shell” $str11   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 $arr30       5 6 14
/=$v = 12 $v /= 6 $v $arr = 5, 6, 7 $arr[1] *= 2 $arr2       5 3 7
%=$v = 12 $v %= 7 $v5
++$v1 = 5 ++$v1 $v1 $v2 = 5 $v3 = $v2++ $v36       5
$v1 = 5 –$v1 $v1 $v2 = 5 $v3 = $v2– $v34       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 OperatorsExpressionsOutput
-eq5 -eq 5True
-ne4 -ne 5True
-gt6 -gt 5True
-ge6 -ge 5True
-lt5 -lt 6True
-le5 -le 6True
Matching OperatorsExpressionsOutput
-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 OperatorsExpressionsOutput
-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 OperatorsExpressionsOutput
-replace“Powershell” -replace “s” “S”PowerShell
-ireplace“Powershell” -replace “s” “S”PowerShell
-creplace (Case Sensitive)“Powershell” -replace “S” “s”PowerShell
Type OperatorsExpressionsOutput
-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 OperatorsExpressionsOutput
-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
*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.

NotationDescriptionExample (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.

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’, 4Sun ,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’, 4Sun ,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.

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.

  1. -is operator returns true when input is .Net Framework type instance else returns false.
  2. -isnot operator returns true when input is not .Net Framework type instance else returns false.
  3. -as converts input to not .Net Framework type.
-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.

Display output from the services Get-Service xbgm | Format-Table -Property name, statusName  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

Up ↑

%d bloggers like this: