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
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.
PS C:\> 10+20 30
PS C:\> 5%2 1
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 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:
|-ge||greater than or equal|
|-le||less than or equal|
PS C:\> $a, $b = 10, 20 PS C:\> $a -eq $b False PS C:\> $a -lt $b True
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))
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 #||Description||Introduced in||Write Cmdlet|
|1||Success stream||PowerShell 2.0|
|2||Error stream||PowerShell 2.0|
|3||Warning stream||PowerShell 3.0|
|4||Verbose stream||PowerShell 3.0|
|5||Debug stream||PowerShell 3.0|
|6||Information stream||PowerShell 5.0|
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.
|Send specified stream to a file.|
|Append specified stream to a file.|
|Redirects the specified stream to the Success stream.|
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.
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
The Boolean type operators (
-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.
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
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.
- How To Effectively Use PowerShell Variables
- PowerShell Alias
- PowerShell Parameters
- Transfer Files Between Windows & Linux Machine Using PowerShell
- How to prepare for Microsoft AZ-900
- AZ-900 Exam Practice Questions