Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
Once you have defined your data, the next step is to work with it.
The arithmetic operators let you perform mathematical operations on your data, as shown in Table A-5.
The System.Math
class in the .NET Framework offers many powerful operations in addition to the native operators supported by PowerShell:
PS > [Math]::Pow([Math]::E, [Math]::Pi) 23.1406926327793
See “Working with the .NET Framework” to learn more about using PowerShell to interact with the .NET Framework.
Operator | Meaning |
---|---|
+ |
The addition operator:
When used with numbers, returns their sum. When used with strings, returns a new string created by appending the second string to the first. When used with arrays, returns a new array created by appending the second array to the first. When used with hashtables, returns a new hashtable created by merging the two hashtables. Since hashtable keys must be unique, PowerShell returns an error if the second hashtable includes any keys already defined in the first hashtable. When used with any other type, PowerShell uses that type’s addition operator ( |
- |
The subtraction operator:
When used with numbers, returns their difference. This operator does not apply to strings, arrays, or hashtables. When used with any other type, PowerShell uses that type’s subtraction operator ( |
* |
The multiplication operator:
When used with numbers, returns their product. When used with strings ( When used with arrays ( This operator does not apply to hashtables. When used with any other type, PowerShell uses that type’s multiplication operator ( |
/ |
The division operator:
When used with numbers, returns their quotient. This operator does not apply to strings, arrays, or hashtables. When used with any other type, PowerShell uses that type’s division operator |
% |
The modulus operator:
When used with numbers, returns the remainder of their division. This operator does not apply to strings, arrays, or hashtables. When used with any other type, PowerShell uses that type’s modulus operator ( |
+= -= *= /= %= |
Assignment operators:
These operators match the simple arithmetic operators (+, -, *, /, and %) but store the result in the variable on the lefthand side of the operator. It is a short form for
|
The logical operators let you compare Boolean values, as shown in Table A-6.
Operator | Meaning |
---|---|
|
Logical AND:
Returns You can combine several
PowerShell implements the |
|
Logical OR:
Returns You can combine several
PowerShell implements the |
|
Logical exclusive OR:
Returns Returns |
|
Logical NOT: -not
Returns |
The binary operators, listed in Table A-7, let you apply the Boolean logical operators bit by bit to the operator’s arguments. When comparing bits, a 1 represents $true
, whereas a 0 represents $false
.
Operator | Meaning |
---|---|
|
Binary AND:
Returns a number where bits are set to 1 if the bits of the lefthand and righthand arguments at that position are both 1. All other bits are set to 0. For example: PS > $int1 = 0b110110110 PS > $int2 = 0b010010010 PS > $result = $int1 -band $int2 PS > [Convert]::ToString($result, 2) 10010010 |
|
Binary OR:
Returns a number where bits are set to 1 if either of the bits of the lefthand and righthand arguments at that position is 1. All other bits are set to 0. For example: PS > $int1 = 0b110110110 PS > $int2 = 0b010010010 PS > $result = $int1 -bor $int2 PS > [Convert]::ToString($result, 2) 110110110 |
|
Binary exclusive OR:
Returns a number where bits are set to 1 if either of the bits of the lefthand and righthand arguments at that position is 1, but not if both are. All other bits are set to 0. For example: PS > $int1 = 0b110110110 PS > $int2 = 0b010010010 PS > $result = $int1 -bxor $int2 PS > [Convert]::ToString($result, 2) 100100100 |
|
Binary NOT: -bnot
Returns a number where bits are set to 1 if the bit of the righthand (and only) argument at that position is set to 1. All other bits are set to 0. For example: PS > $int1 = 0b110110110 PS > $result = -bnot $int1 PS > [Convert]::ToString($result, 2) 11111111111111111111111001001001 |
|
Binary shift left:
Shifts the bits of a number to the left For example: PS > $int1 = 438 PS > [Convert]::ToString($int1, 2) 110110110 PS > $result = $int1 -shl 5 PS > [Convert]::ToString($result, 2) 11011011000000 |
|
Binary shift right:
Shifts the bits of a number to the right For example: PS > $int1 = -2345 PS > [Convert]::ToString($int1, 2) 11111111111111111111011011010111 PS > $result = $int1 -shr 3 PS > [Convert]::ToString($result, 2) 11111111111111111111111011011010 |
PowerShell supports several other simple operators, as listed in Table A-8.
Operator | Meaning |
---|---|
|
The replace operator:
Returns a new string, where the text in
Returns a new string, where the text in By default, PowerShell performs a case-insensitive comparison. The If the regular expression pattern contains named captures or capture groups, the replacement string may reference those as well. For example: PS > "Hello World" -replace "(.*) (.*)",'$2 $1' World Hello If For more information on the details of regular expressions, see Appendix B. |
|
The format operator:
Returns a string where the format items in the format string have been replaced with the text equivalent of the values in the value array. For example: PS > "{0:n0}" -f 1000000000 1,000,000,000 The format string for the format operator is exactly the format string supported by the .NET For more details about the syntax of the format string, see Appendix D. |
|
The type conversion operator:
Returns For example: PS > 3/2 -as [int] 2 PS > $result = "Hello" -as [int] PS > $result -eq $null True |
|
The unary split operator: -split
Breaks the given input string into an array, using whitespace ( For example: PS > -split " Hello World " Hello World The binary split operator: "Input String" -split " Breaks the given input string into an array, using the given
For example: PS > "1a2B3" -split "[a-z]+",0,"IgnoreCase" 1 2 3 |
|
The unary join operator: -join
Combines the supplied items into a single string, using no separator. For example: PS > -join ("a","b") ab The binary join operator:
Combines the supplied items into a single string, using PS > ("a","b") -join ", " a, b |