Professional Documents
Culture Documents
About Arithmetic Operators - Help
About Arithmetic Operators - Help
about_Arithmetic_Operators
SHORT DESCRIPTION
Describes the operators that perform arithmetic in Windows PowerShell.
LONG DESCRIPTION
Arithmetic operators calculate numeric values. You can use one or
more arithmetic operators to add, subtract, multiply, and divide
values, and to calculate the remainder (modulus) of a division operation.
In addition, the addition operator (+) and multiplication operator (*)
also operate on strings, arrays, and hash tables. The addition operator
concatenates the input. The multiplication operator returns multiple copies
of the input. You can even mix object types in an arithmetic statement.
The method that is used to evaluate the statement is determined by the type
of the leftmost object in the expression.
Windows PowerShell supports the following arithmetic operators:
OPERATOR PRECEDENCE
Windows PowerShell processes arithmetic operators in the following order:
Parentheses ()
- (for a negative number)
*, /, %
+, - (for subtraction)
C:\PS> $a = 1,2,3
C:\PS> $b = "A","B,"C"
C:\PS> $a + $b
1
2
3
A
B
C
C:\PS> "file" + 16
file16
C:\PS> $array = 1,2,3
C:\PS> $array + 16
1
2
3
16
C:\PS> $array + "file"
1
2
3
file
C:\PS> "file" * 3
filefilefile
Because the method that is used to evaluate statements is determined by the
leftmost object, addition and multiplication in Windows PowerShell are not
strictly commutative. For example, (a + b) does not always equal (b + a),
and (a * b) does not always equal (b * a).
The following examples demonstrate this principle:
C:\PS> "file" + 2
file2
C:\PS> 2 + "file"
Cannot convert value "file" to type "System.Int32". Error: "Input
string was not in a correct format."
At line:1 char:4
+ 2 + <<<< "file"
C:\PS> "file" * 3
filefilefile
C:\PS> 3 * "file"
Cannot convert value "file" to type "System.Int32". Error: "Input
string was not in a correct format."
At line:1 char:4
+ 3 * <<<< "file"
Hash tables are a slightly different case. You can add hash tables. And,
you can add a hash table to an array. However, you cannot add any other
type to a hash table.
The following examples show how to add hash tables to each other and to
other objects:
C:\PS> $hash1 = @{a=1; b=2; c=3}
C:\PS> $hash2 = @{c1="Server01"; c2="Server02"}
C:\PS> $hash1 + $hash2
Name Value
---- -----
c2 Server02
a 1
b 2
c1 Server01
c 3
C:\PS> $hash1 + 2
You can add another hash table only to a hash table.
At line:1 char:9
+ $hash1 + <<<< 2
C:\PS> 2 + $hash1
Cannot convert "System.Collections.Hashtable" to "System.Int32".
At line:1 char:4
+ 2 + <<<< $hash1
The following examples demonstrate that you can add a hash table to an
array. The entire hash table is added to the array as a single object.
The following example shows that you cannot add hash tables that contain
the same key:
C:\PS> $hash1 = @{a=1; b=2; c=3}
C:\PS> $hash2 = @{c="red"}
C:\PS> $hash1 + $hash2
Bad argument to operator '+': Item has already been added.
Key in dictionary: 'c' Key being added: 'c'.
At line:1 char:9
+ $hash1 + <<<< $hash2
Although the addition operators are very useful, use the assignment
operators to add elements to hash tables and arrays. For more information
see about_assignment_operators. The following examples use the +=
assignment operator to add items to an array:
C:\PS> $array
1
2
3
C:\PS> $array + "file"
1
2
3
file
C:\PS> $array
1
2
3
C:\PS> $array += "file"
C:\PS> $array
1
2
3
file
C:\PS> $hash1
Name Value
---- -----
a 1
b 2
c 3
C:\PS> $hash1 += @{e = 5}
C:\PS> $hash1
Name Value
---- -----
a 1
b 2
e 5
c 3
C:\PS> get-date
Wednesday, January 02, 2008 1:28:42 PM
EXAMPLES
The following examples show how to use the arithmetic operators in
Windows PowerShell:
C:\PS> 1 + 1
2
C:\PS> 1 - 1
0
C:\PS> -(6 + 3)
-9
C:\PS> 6 * 2
12
C:\PS> 7 / 2
3.5
C:\PS> 7 % 2
1
C:\PS> w * 3
www
C:\PS> 3 * "w"
Cannot convert value "w" to type "System.Int32". Error: "Input string wa
s not
in a correct format."
At line:1 char:4
+ 3 * <<<< "w"
PS C:\ps-test> "Windows" + " " + "PowerShell"
Windows PowerShell
PS C:\ps-test> $a = "Windows" + " " + "PowerShell"
PS C:\ps-test> $a
Windows PowerShell
C:\PS> $a[0]
W
C:\PS> $a = "TestFiles.txt"
C:\PS> $b = "C:\Logs\"
C:\PS> $b + $a
C:\Logs\TestFiles.txt
C:\PS> $a = 1,2,3
C:\PS> $a + 4
1
2
3
4
C:\PS> $servers = @{0 = "LocalHost"; 1 = "Server01"; 2 = "Server02"}
C:\PS> $servers + @{3 = "Server03"}
Name Value
---- -----
3 Server03
2 Server02
1 Server01
0 LocalHost
C:\PS> $servers
Name Value
---- -----
2 Server02
1 Server01
0 LocalHost
C:\PS> $servers += @{3 = "Server03"} #Use assignment operator
C:\PS> $servers
Name Value
---- -----
3 Server03
2 Server02
1 Server01
0 LocalHost
SEE ALSO
about_arrays
about_assignment_operators
about_comparison_operators
about_hash_tables
about_operators
about_variables
Get-Date
New-TimeSpan