TOPIC 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 Example -------- ----------- ------- + Adds integers; concatenates strings, 6+2 arrays, and hash tables. "file" + "name" - Subtracts one value from another 6-2 value. (get-date).date - 1 - Makes a number a negative number. -6+2 -4 * Multiplies integers; copies strings 6*2 and arrays the specified number of "w" * 3 times. / Divides two values. 6/2 % Returns the remainder of a division 7%2 operation. 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]. ADDING AND MULTIPLYING NON-NUMERIC TYPES You can add numbers, strings, arrays, and hash tables. And, you can multiply numbers, strings, and arrays. However, you cannot multiply hash tables. When you add strings, arrays, or hash tables, the elements are concatenated. When you concatenate collections, such as arrays or hash tables, a new object is created that contains the objects from both collections. If you try to concatenate hash tables that have the same key, the operation fails. For example, the following commands create two arrays and then add them: C:\PS> $a = 1,2,3 C:\PS> $b = "A","B,"C" C:\PS> $a + $b 1 2 3 A B C You can also perform arithmetic operations on objects of different types. The operation that Windows PowerShell performs is determined by the Microsoft .NET Framework type of the leftmost object in the operation. Windows PowerShell tries to convert all the objects in the operation to the .NET Framework type of the first object. If it succeeds in converting the objects, it performs the operation appropriate to the .NET Framework type of the first object. If it fails to convert any of the objects, the operation fails. The following example demonstrates the use of the addition and multiplication operators in operations that include different object types: 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. C:\PS> $array = 1,2,3 C:\PS> $array + $hash1 1 2 3 Name Value ---- ----- a 1 b 2 c 3 C:\PS> $sum = $array + $hash1 C:\PS> $sum.count 4 C:\PS> $sum[3] Name Value ---- ----- a 1 b 2 c 3 PS C:\ps-test> $sum + $hash2 1 2 3 Name Value ---- ----- a 1 b 2 c 3 c2 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 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 Windows PowerShell automatically selects the .NET Framework numeric type that best expresses the result without losing precision. For example: C:\PS> 2 + 3.1 5.1 C:\PS> (2). GetType().FullName System.Int32 C:\PS> (2 + 3.1).GetType().FullName System.Double If the result of an operation is too large for the type, the type of the result is widened to accom...
magdula294