Professional Documents
Culture Documents
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 Description
-------- ----------+
Adds integers; concatenates strings,
arrays, and hash tables.
Example
------6+2
"file" + "name"
6-2
(get-date).date - 1
-6+2
-4
6*2
"w" * 3
6/2
7%2
OPERATOR PRECEDENCE
Windows PowerShell processes arithmetic operators in the following order:
Parentheses ()
- (for a negative number)
*, /, %
+, - (for subtraction)
Windows PowerShell processes the expressions from left to right according
to the precedence rules. The following examples show the effect of the
precedence rules:
C:\PS> 3+6/3*4
11
C:\PS> 10+4/2
12
C:\PS> (10+4)/2
7
C:\PS> (3+3)/ (1+1)
3
The order in which Windows PowerShell evaluates expressions might differ
from other programming and scripting languages that you have used. The
following example shows a complicated assignment statement.
C:\PS> $a = 0
C:\PS> $b = 1,2
C:\PS> $c = -1,-2
C:\PS> $b[$a] = $c[$a++]
C:\PS> $b
1
-1
In this example, the expression $a++ is evaluated before $c[$a++].
Evaluating $a++ changes the value of $a. The variable $a in $b[$a]
equals 1, not 0, so the statement assigns a value to $b[1], not $b[0].
Value
----Server02
1
2
Server01
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.
C:\PS> $array = 1,2,3
C:\PS> $array + $hash1
1
2
3
Name
---a
b
c
Value
----1
2
3
Value
----1
2
3
Name
---a
b
c
c2
Value
----1
2
3
Server02
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
---a
b
c
C:\PS> $hash1 += @{e = 5}
C:\PS> $hash1
Value
----1
2
3
Name
---a
b
e
c
Value
----1
2
5
3
C:\PS>
C:\PS>
C:\PS>
C:\PS>
$a
$b
$c
$a
=
=
=
+
"Windows "
"PowerShell "
2
$b + $c
Windows PowerShell 2
ARITHMETIC OPERATORS AND COMMANDS
Typically, you use the arithmetic operators in expressions with numbers,
strings, and arrays. However, you can also use arithmetic operators with
the objects that commands return and with the properties of those objects.
The following examples show how to use the arithmetic operators in
expressions with Windows PowerShell commands:
C:\PS> get-date
Wednesday, January 02, 2008 1:28:42 PM
C:\PS> $day = new-timespan -day 1
C:\PS> get-date + $day
Thursday, January 03, 2008 1:34:52 PM
C:\PS> get-process | where {($_.ws * 2) -gt 50mb}
Handles NPM(K)
PM(K)
WS(K) VM(M) CPU(s)
------- -------------- ----- -----1896
39
50968
30620 264 1,572.55
12802
78 188468
81032 753 3,676.39
660
9
36168
26956 143
12.20
561
14
6592
28144 110 1,010.09
3476
80
34664
26092 234 ...45.69
967
30
58804
59496 416 930.97
Id
-1104
5676
988
496
876
2508
ProcessName
----------explorer
OUTLOOK
powershell
services
svchost
WINWORD
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