PowerShellish Arithmetic or why “Text”+2 not equal 2+”Text” part1

Powershellish Arithmetic is PowerShell’s implementation of simple math rules. In math simple actions like addition and multiplication are always commutative (a+b=b+a) but PowerShell does not obey those rules.

The biggest difference of Powershellish Arithmetic is in adding and multiplying non numeric types.

Powershellish Arithmetic: Addition
Powershellish Arithmetic: Multiplication

Here is the explanation from official Microsoft Documentation:

When two non numeric types are added or multiplied Powershell evaluates .Net type of the leftmost object and then determines what it should do. It tries to convert all the objects in the operation to the .NET Framework type of the first object. If it conversion succeeds, it performs the operation appropriate to the .NET Framework type of the first object. If it not – the operation fails.

Powershellish Arithmetic: Addition

Let’s take a closer look at our first example:

"Hello"+13

First object’s type is String. If you wonder how to find it out just pipe object to Get-Member:

Second object’s type is Integer:

Left most object in our case is string, so both object are treated as strings and “+” operator for strings means concatenation. And as result we get concatenated string:

Hello13

When the leftmost object is integer(number 13 in our case) then Powershell treats “+” operator as regular math addition and tries to cast another object(which has string type to integer) and then add it to the first. String “Hello” cannot be converted to Integer as it do not contain any numbers. And we receive an error as result of this operation.

Powershellish Arithmetic: Multiplication

Powershell follows same algorithm for multiplication. In our first multiplication example leftmost object is String and based on that Powershell will print string “Reader” four times:

"Reader"*4
ReaderReaderReaderReader

When number 4 is the leftmost object “*” operator is treated as regular math multiplication operator and tries to convert rightmost object to integer and multiply it to 4. And surely conversion of “Reader” to integer fails and we receive an error.

Other non numeric types

You can also add arrays and hash tables and multiply array to a number (type integer) only. Hashtables do not support multiplication.

When adding two arrays “+” operator is performing concatenation. New object is created that contains object from both participants.

Powershellish Arithmetic: Adding Arrays
$NumbersArray=@(13,4,8848,8)
$StringsArray=@("nature","planet")

$NumbersArray+$StringsArray
13
4
8848
8
nature
planet

$StringsArray+$NumbersArray
nature
planet
13
4
8848
8

If you multiply array to a number it will create new object with specified number of copies of the array:

$StringsArray=@("nature","planet")
$StringsArray*4
nature
planet
nature
planet
nature
planet
nature
planet

Addition of hash tables behaves the same way as arrays addition with on elittle condition. You cannot add hastables if they have same keys. Otherwise you will receive new hash table with keys and values from both participants of the addition operation:

Powershellish Arithmetic: Adding has tables

When you will try to add hash tables which contains duplicate keys you will receive an error saying just that:

Thanks a lot for reading.


Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.