The addition operator may be used to add numeric values:
2.71828 + 3.14159
It may also be used to concatenate strings:
'hello' + ' ' + 'world'
If an attempt is made to concatenate a string with a number, the number will be converted into a string:
'hello number ' + 1
This style of operation will fail if the number is used first. PowerShell expects the entire expression to be numeric if that is how it begins:
PS> 1 + ' is the number I like to use'
Cannot convert value "is the number I like to use" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ 1 + ' is the number I like to use'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
The addition operator may be used to add single elements to an existing array. The following expression results in an array containing 1, 2, and 3:
@(1, 2) + 3
Joining arrays with the addition operator is simple. Each of the following three examples creates an array and each array contains the values 1, 2, 3, and 4:
@(1, 2) + @(3, 4) (1, 2) + (3, 4) 1, 2 + 3, 4
Hashtables may be joined in a similar manner:
@{key1 = 1} + @{key2 = 2}
The addition operation will fail if keys are duplicated as part of the addition operation:
PS> @{key1 = 1} + @{key1 = 2}
Item has already been added. Key in dictionary: 'key1' Key being added: 'key1'
At line:1 char:1
+ @{key1 = 1} + @{key1 = 2}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
The subtraction operator may only be used for numeric expressions. The results of the following expressions are 3 and -18, respectively:
5 - 2 2 - 20