PowerShell provides different types of operators and through this article, we are going to discuss about Arithmetic Operators. Arithmetic Operators are used to compute numerical values. Before stating this article, would like to emphasis on one thing; that is, PowerShell is built on .Net Core; internally it uses .Net Core classes.
We need to understand little bit about Classes; and operator overloading. Everything in PowerShell, is of an object of a Class. For example, when we use a string, “Hello!”, internally it treated as an object of String class; similarly, when we us a numeric value, 456, internally it treats as an object of Int32 class; and so on.
.Net classes supports operator overloading. The behavior of an operator will depend on it’s overloaded definition in the class. Keep this in mind.
Some of these Arithmetic Operators are used in arrays, hash tables and strings also. We are going to discuss them through this Article.
Addition Operator (+)
This operator is used to add numeric values. We can use this operator with other data types; with strings, arrays and hash tables, it is used to concatenate the inputs. Here are some examples with this operator;
Expression | Result |
PS C:\> 5 + 7 | 12 |
PS C:\> “Me” + ” on Mars” | Me on Mars |
PS C:\> (1, 2, 3) + (5, 6, 7) | 1 2 3 5 6 7 |
PS C:\> $numbers = @{1 = “One”; 2 = “Two”; 3 = “Three”} PS C:\> $fruits = @{A = “Apple”; B = “Banana”; C = “Cherry”} PS C:\> $numbers + $fruits | Name Value ---- ----- B Banana C Cherry 3 Three 2 Two 1 One A Apple |
Interesting thing is; we can use this operator to concatenate string values with numeric values too. But, we have to be cautious when we are using this operator with mixed values. When we do this, the order where we are using the numerical value is important. For example, below expression will throw the Error;
PS C:\> 2 + "Hello! "
Cannot convert value "Hello!" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ 2 + "Hello! "
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
Why it throws an Error? Above expression, looks for an overloading operator (+) in Int32
class (because left side value of + operator is numeric value); to perform the operation with the given string “Hello!”. As the operator was not overloaded in Int32
class; the above expression throws an Error.
But, when we re-write the above expression, like below; it works.
PS C:\> "Hello! " + 2
Why it worked? Because, “Hello! ” is an object of String
class. And it has overloaded version of the operator (+) to concatenate the values.
That is why; we have to be more cautious on the order in the expression where we are using the Numerical value when we are using them with mixed data types.
We can not use this operator to concatenate a hash table with another type. Hash table can be added with another hash table only. For example, below expression to attempt to add numeric value to hash table, throws an Error;
PS C:\> $numbers + 2
A hash table can only be added to another hash table.
At line:1 char:1
+ $numbers + 2
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : AddHashTableToNonHashTable
Subtraction Operator (-)
Subtract one numeric value from another can be done through this operator. For example, 5 – 2 will give the result 3.
Multiplication Operator (*)
Multiplication operator is useful to multiply one numeric value with another value. For example, 112 * 2 will produce the result 224.
We can use this operator with strings and arrays too, to return the inputs multiple times. Here are some examples;
Expression | Result |
PS C:\> $str = “Guava fruit is good for health! ” PS C:\> $str * 2 | Guava fruit is good for health! Guava fruit is good for health! |
PS C:\> 5 * 2 | 10 |
PS C:\> $fruits = (“Apple”, “Banana”, “Cherry”) PS C:\> $v = $fruits * 2 PS C:\> $v | Apple Banana Cherry Apple Banana Cherry |
We can not use this operator with hash tables. If we attempt to do, we will see below Error;
PS C:\> $numbers = @{1 = "One"; 2 = "Two"; 3 = "Three"}
PS C:\> $numbers * 2
Method invocation failed because [System.Collections.Hashtable] does not contain a method named 'op_Multiply'
At line:1 char:1
+ $numbers * 2
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Multiply:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Division Operator (/)
A division operator calculates the division of one numeric value with another value. For example, 7 / 2 will give the value 1.4.
Remainder Operator (%)
Remainder or Modulus operator is used to get the remainder of a division operation. For example, 9 % 2 gives the value 1.
[..] David