PowerShell Operators

Arithmetic Operators

OperatorDescriptionExample UsageResult
+Adds two numbers or concatenates strings.5 + 38
-Subtracts one number from another.10 - 46
*Multiplies two numbers.6 * 742
/Divides one number by another.15 / 35
%Returns the remainder of a division operation.10 % 31
++Increments a variable’s value by one (post-increment).$a++ (if $a = 5)$a = 6
--Decrements a variable’s value by one (post-decrement).$b-- (if $b = 5)$b = 4

Assignment Operators

OperatorDescriptionExample UsageResult
=Assigns a value to a variable.$a = 10$a = 10
+=Adds and assigns the value to a variable.$a += 5$a = 15 (if $a = 10)
-=Subtracts and assigns the value to a variable.$a -= 3$a = 7 (if $a = 10)
*=Multiplies and assigns the value to a variable.$a *= 2$a = 20 (if $a = 10)
/=Divides and assigns the value to a variable.$a /= 2$a = 5 (if $a = 10)
%=Modulo and assigns the value to a variable.$a %= 4$a = 2 (if $a = 10)

Comparison Operators

OperatorDescriptionExample UsageResult
-eqChecks if two values are equal.5 -eq 5True
-neChecks if two values are not equal.5 -ne 3True
-gtChecks if the left value is greater than the right.10 -gt 5True
-geChecks if the left value is greater than or equal to the right.5 -ge 5True
-ltChecks if the left value is less than the right.3 -lt 10True
-leChecks if the left value is less than or equal to the right.5 -le 10True
-likeMatches a string with a wildcard pattern.'PowerShell' -like '*Shell*'True
-notlikeDoes not match a string with a wildcard pattern.'PowerShell' -notlike 'Cmd*'True
-matchMatches a string with a regular expression.'abc123' -match '\d{3}'True
-notmatchDoes not match a string with a regular expression.'abc123' -notmatch '\d{4}'True
-containsChecks if a collection contains a value.@(1,2,3) -contains 2True
-notcontainsChecks if a collection does not contain a value.@(1,2,3) -notcontains 4True
-inChecks if a value exists in a collection.5 -in @(2, 4, 5)True
-notinChecks if a value does not exist in a collection.6 -notin @(2, 4, 5)True
-replaceReplaces text matching a regular expression pattern.'PowerShell' -replace 'Shell', 'Cmd'PowerCmd

Logical Operators

OperatorDescriptionExample UsageResult
-andReturns true if both statements are true.$true -and $trueTrue
-orReturns true if one of the statements is true.$true -or $falseTrue
-notReverses the logical state of its operand.-not $falseTrue
!Alias for -not, reverses the logical state.!$trueFalse
-bandPerforms a bitwise AND operation.5 -band 31
-borPerforms a bitwise OR operation.5 -bor 37
-bxorPerforms a bitwise XOR operation.5 -bxor 36

Redirection Operators

OperatorDescriptionExample UsageResult
>Redirects output to a file (overwrites existing content).Get-Process > output.txtCreates or overwrites output.txt with the process list.
>>Redirects output to a file (appends to existing content).Get-Process >> output.txtAppends process list to output.txt.
2>Redirects error output to a file (overwrites existing content).Get-Process 2> errors.txtCreates or overwrites errors.txt with error messages.
2>>Redirects error output to a file (appends to existing content).Get-Process 2>> errors.txtAppends error messages to errors.txt.
2>&1Redirects error output to the same location as standard output.Get-Process 2>&1Combines standard output and error messages.
``Pipes the output of one command into another command.`Get-Process

Type Operators

OperatorDescriptionExample UsageResult
-isChecks if an object is a specific type.'Hello' -is [string]True
-isnotChecks if an object is not a specific type.'Hello' -isnot [int]True
-asAttempts to convert an object to a specific type.5 -as [string]"5"

Range Operator

OperatorDescriptionExample UsageResult
..Generates a sequence of numbers.1..51, 2, 3, 4, 5

Split and Join Operators

OperatorDescriptionExample UsageResult
-splitSplits a string into an array.'one,two,three' -split ','one, two, three
-joinJoins an array into a string.('one', 'two', 'three') -join ','"one,two,three"

Unary Operators

OperatorDescriptionExample UsageResult
+Unary plus, returns the value of the operand.+55
-Unary minus, negates the value of the operand.-5-5

Special Operators

OperatorDescriptionExample UsageResult
&Runs a command, script, or script block.& 'C:\script.ps1'Runs script.ps1.
. (dot sourcing)Runs a script in the current scope.. .\script.ps1Executes script.ps1 in the current session, preserving any functions or variables.
::Accesses static members of a class.[System.Math]::Sqrt(25)5 (Square root of 25).
?Alias for Where-Object in pipelines.Get-Process ? { $_.CPU -gt 100 }Filters processes with CPU usage greater than 100.
%Alias for ForEach-Object in pipelines.Get-Process % { $_.Name }Outputs the names of processes.
This table provides a comprehensive overview of PowerShell operators, including their functions, example usage, and typical results. These operators are fundamental to performing various tasks in PowerShell, from simple arithmetic to complex scripting operations.