PowerShell Operators with explanation and examples

Spread The Knowledge

An operator is a language element that you can use in a command or expression. PowerShell supports several types of operators to help you manipulate values. Hence Today we will discuss all the PowerShell Operators With Explanation And Examples PowerShell has the following operators available:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Redirection Operators
  • Split and Join Operators
  • Type Operators
  • Subexpression operator
types of operators in powershell
PowerShell Operators

Arithmetic Operators

Use arithmetic operators (+-*/%) to calculate values in a command or expression. With these operators, you can add, subtract, multiply, or divide values, and calculate the remainder (modulus) of a division operation.

Example:

PS C:\> 10+20
30
PS C:\> 5%2
1

Assignment Operators

You can Use assignment operators (=+=-=*=/=%=) to assign, change, or append values to variables. You can combine arithmetic operators with the assignment to assign the result of the arithmetic operation to a variable.

For Example, Below statement will add the value of $a and $b and will replace the value of $a with the result of addition.

$a += $b

Comparison Operators

Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.

PowerShell includes the following comparison operators:

TypeOperatorsDescription
Equality-eqequals
-nenot equals
-gtgreater than
-gegreater than or equal
-ltless than
-leless than or equal
Comparison Operators

Example:

PS C:\> $a, $b = 10, 20
PS C:\> $a -eq $b
False
PS C:\> $a -lt $b
True

Logical Operators

The PowerShell logical operators connect expressions and statements, allowing you to use a single expression to test for multiple conditions.

For example, the following statement uses the and operator and the or operator to connect three conditional statements. The statement is true only when the value of $a is greater than the value of $b, and either $a or $b is less than 20.

($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))

Redirection Operators

Before getting into Redirection Operators, you need to understand the PowerShell streams first.

PowerShell provides multiple output streams. The streams provide channels for different types of messages. You can write to these streams using the associated cmdlet or redirection. For more information, see about_Redirection.

The PowerShell supports the following output streams.

Stream #DescriptionIntroduced inWrite Cmdlet
1Success streamPowerShell 2.0Write-Output
2Error streamPowerShell 2.0Write-Error
3Warning streamPowerShell 3.0Write-Warning
4Verbose streamPowerShell 3.0Write-Verbose
5Debug streamPowerShell 3.0Write-Debug
6Information streamPowerShell 5.0Write-Information
PowerShell Output Streams

PowerShell redirection operators

The redirection operators are as follows, where ‘n' represents the stream number. The Success stream ( 1 ) is the default if no stream is specified.

OperatorDescriptionSyntax
>Send specified stream to a file.n>
>>Append specified stream to a file.n>>
>&1Redirects the specified stream to the Success stream.n>&1
Redirection Operators

Split and Join Operators

The Split operator splits one or more strings into substrings. You can change the following elements of the Split operation:

  • Delimiter. The default is whitespace, but you can specify characters, strings, patterns, or script blocks that specify the delimiter. The Split operator in PowerShell uses a regular expression in the delimiter, rather than a simple character.
  • Maximum number of substrings. The default is to return all substrings. If you specify a number less than the number of substrings, the remaining substrings are concatenated in the last substring.
  • Options that specify the conditions under which the delimiter is matched, such as SimpleMatch and Multiline.

Examples:

PS C:\> "aman deep singh" -split " "
aman
deep
singh
PS C:\> "aman_deep_singh" -split "_"
aman
deep
singh

If you want to include delimiter also, then you can enclose the delimiter in braces:

PS C:\> "aman_deep_singh" -split "(_)"
aman
_
deep
_
singh

The join operator concatenates a set of strings into a single string. The strings are appended to the resulting string in the order that they appear in the command.

-join ("Windows", "PowerShell", "7.0") 
WindowsPowerShell7.0
"Windows", "PowerShell", "7.0" -join " "
Windows PowerShell 7.0

Type Operators

The Boolean type operators (-is and -isNot) tell whether an object is an instance of a specified .NET type. The -is operator returns a value of TRUE if the type matches and a value of FALSE otherwise. The -isNot operator returns a value of FALSE if the type matches and a value of TRUE otherwise.

Examples:

PS C:\WINDOWS\system32> $a = 30
PS C:\WINDOWS\system32> $a -is [int]
True
PS C:\WINDOWS\system32> $str = "aman"
PS C:\WINDOWS\system32> $str -isnot [int]
True

Subexpression Operator $()

Returns the result of one or more statements. For a single result, returns a scalar. For multiple results, returns an array. Use this when you want to use an expression within another expression. For example, to embed the results of command in a string expression.

PS C:\> "Today is Get-Date"
Today is Get-Date
PS C:\> "Today is $(Get-Date)"
Today is 12/09/2020 18:55:49

Range operator ..

Represents the sequential integers in an integer array, given an upper, and lower boundary. We can use the range operator to create an array on the go.

PS C:\> 1..5 | Write-Host
1
2
3
4
5
PS C:\> $arr = 1..10
PS C:\> $arr
1
2
3
4
5
6
7
8
9
10

To create a range of characters, enclose the boundary characters in quotes.

PS C:\> 'a'..'f'
a
b
c
d
e
f

Note: To use the range operator with characters, you will need atleast PowerShell 6.0 version.

Source: Microsoft

Up Next:


Spread The Knowledge

Leave a Comment