How to use PowerShell Operators

By FoxLearn 8/27/2024 1:42:45 AM   135
PowerShell offers a variety of operators for performing operations on data. These operators fall into several categories, including arithmetic, comparison, logical, and more.

Assignment Operators

OperatorExampleDescription
=$x = 5Assigns a value to a variable.
+=

$x += 3

Adds the right-hand side value to the left-hand side variable and assigns the result to the variable
-=

$x -= 3

Subtracts the right-hand side value from the left-hand side variable and assigns the result to the variable
*=

$x *= 3

Multiplies the left-hand side variable by the right-hand side value and assigns the result to the variable
/=

$x /= 3

Divides the left-hand side variable by the right-hand side value and assigns the result to the variable
%=

$x %= 3

Computes the modulus (remainder) of dividing the left-hand side variable by the right-hand side value and assigns the result to the variable

Arithmetic Operators

These operators are used for mathematical calculations

OperatorExampleDescription
+5 + 3 results in 8Addition
-5 - 3 results in 2Subtraction
*5 * 3 results in 15Multiplication
/5 / 2 results in 2.5Division
%5 % 2 results in 1Modulus (remainder after division)
++$x++ increments $x by 1Increment
--$x-- decrements $x by 1Decrement

Comparison Operators

These operators compare values and return a Boolean result

OperatorExampleDescription
-eq5 -eq 5 returns TrueEqual
-ne5 -ne 3 returns TrueNot equal
-gt5 -gt 3 returns TrueGreater than
-lt5 -lt 3 returns FalseLess than
-ge5 -ge 5 returns TrueGreater than or equal to
-le5 -le 6 returns TrueLess than or equal to
-like"hello" -like "h*" returns TrueMatches a pattern
-notlike"hello" -notlike "h*" returns FalseDoes not match a pattern
-match"hello" -match "ell" returns TrueMatches a regular expression
-notmatch"hello" -notmatch "abc" returns TrueDoes not match a regular expression

Logical Operators

These operators are used to perform logical operations

OperatorExampleDescription
-and($true -and $false) returns FalseLogical AND
-or($true -or $false) returns TrueLogical OR
-xor($true -xor $false) returns TrueLogical XOR
-not-not $true returns FalseLogical NOT
!!$true returns FalseNegation

Array Operators

These operators are used with arrays

OperatorExampleDescription
+$a + $b concatenates arrays $a and $bArray concatenation
-$a - $b returns elements in $a but not in $bArray subtraction

Type Operators

These operators are used for type checking and conversion

OperatorExampleDescription
-is$var -is [int] checks if $var is an integerChecks if a variable is of a specified type.
-as$var -as [int] attempts to cast $var to an integerTries to convert a variable to a specified type.

Redirection Operators

These operators are used for redirecting output

OperatorExampleDescription
>Get-Process > processes.txtRedirect output to a file
>>Get-Process >> processes.txtAppend output to a file
2>Get-Process 2> errors.txtRedirect error output to a file
2>>Get-Process 2>> errors.txtAppend error output to a file