TOPIC about_assignment_operators SHORT DESCRIPTION Describes how to use operators to assign values to variables. LONG DESCRIPTION Assignment operators assign one or more values to a variable. They can perform numeric operations on the values before the assignment. Windows PowerShell supports the following assignment operators. Operator Description ------- ----------- = Sets the value of a variable to the specified value. += Increases the value of a variable by the specified value, or appends the specified value to the existing value. -= Decreases the value of a variable by the specified value. *= Multiplies the value of a variable by the specified value, or appends the specified value to the existing value. /= Divides the value of a variable by the specified value. %= Divides the value of a variable by the specified value and then assigns the remainder (modulus) to the variable. ++ Increases the value of a variable, assignable property, or array element by 1. -- Decreases the value of a variable, assignable property, or array element by 1. SYNTAX The syntax of the assignment operators is as follows: <assignable-expression> <assignment-operator> <value> Assignable expressions include variables and properties. The value can be a single value, an array of values, or a command, expression, or statement. The increment and decrement operators are unary operators. Each has prefix and postfix versions. <assignable-expression><operator> <operator><assignable-expression> The assignable expression must a number or it must be convertible to a number. ASSIGNING VALUES Variables are named memory spaces that store values. You store the values in variables by using the assignment operator (=). The new value can replace the existing value of the variable, or you can append a new value to the existing value. The basic assignment operator is the equal sign (=)(ASCII 61). For example, the following statement assigns the value Windows PowerShell to the $MyShell variable: $MyShell = "Windows PowerShell" When you assign a value to a variable in Windows PowerShell, the variable is created if it did not already exist. For example, the first of the following two assignement statements creates the $a variable and assigns a value of 6 to $a. The second assignment statement assigns a value of 12 to $a. The first statement creates a new variable. The second statement changes only its value: $a = 6 $a = 12 Variables in Windows PowerShell do not have a specific data type unless you cast them. When a variable contains only one object, the variable takes the data type of that object. When a variable contains a collection of objects, the variable has the System.Object data type. Therefore, you can assign any type of object to the collection. The following example shows that you can add process objects, service objects, strings, and integers to a variable without generating an error: $a = get-process $a += get-service $a += "string" $a += 12 Because the assignment operator (=) has a lower precedence than the pipeline operator (|), parentheses are not required to assign the result of a command pipeline to a variable. For example, the following command sorts the services on the computer and then assigns the sorted services to the $a variable: $a = get-service | sort name You can also assign the value created by a statement to a variable, as in the following example: $a = if ($b -lt 0) { 0 } else { $b } This example assigns 0 to the $a variable if the value of $b is less than 0. It assigns the value of $b to $a if the value of $b is not less than zero. THE ASSIGNMENT OPERATOR (=) The assignment operator (=) assigns values to variables. If the variable already has a value, the assignment operator (=) replaces the value without warning. The following statement assigns the integer value 6 to the $a variable: $a = 6 To assign a string value to a variable, enclose the string value in quotation marks, as follows: $a = "baseball" To assign an array (multiple values) to a variable, separate the values with commas, as follows: $a = "apple", "orange", "lemon", "grape" To assign a hash table to a variable, use the standard hash table notation in Windows PowerShell. Type an at sign (@) followed by key/value pairs that are separated by semicolons (;) and enclosed in braces ({ }). For example, to assign a hash table to the $a variable, type: $a = @{one=1; two=2; three=3} To assign hexadecimal values to a variable, precede the value with "0x". Windows PowerShell converts the hexadecimal value (0x10) to a decimal value (in this case, 16) and assigns that value to the $a variable. For example, to assign a value of 0x10 to the $a variable, type: $a = 0x10 To assign an exponential value to a variable, type the root number, the letter "e", and a number that represents a multiple of 10. For example, to assign a value of 3.1415 to the power of 1,000 to the $a variable, type: $a = 3.1415e3 Windows PowerShell can also convert kilobytes (KB), megabytes (MB), and gigabytes (GB) into bytes. For example, to assign a value of 10 kilobytes to the $a variable, type: $a = 10kb THE ASSIGNMENT BY ADDITION OPERATOR (+=) The assignment by addition operator (+=) either increments the value of a variable or appends the specified value to the existing value. The action depends on whether the variable has a numeric or string type and whether the variable contains a single value (a scalar) or multiple values (a collection). The += operator combines two operations. First, it adds, and then it assigns. Therefore, the following statements are equivalent: $a += 2 $a = ($a + 2) When the variable contains a single numeric value, the += operator increments the existing value by the amount on the right side of the operator. Then, the operator assigns the resulting value to the variable. The following example shows how to use the += operator to increase the value of a variable: C:\PS> $a = 4 C:\PS> $a += 2 C:\PS> $a 6 When the value of the variable is a string, the value on the right side of the operator is appended to the string, as follows: C:\PS> $a = "Windows" C:\PS> $a +- " PowerShell" C:\PS> $a Windows PowerShell When the value of the variable is an array, the += operator appends the values on the right side of the operator to the array. Unless the array is explicitly typed by casting, you can append any type of value to the array, as follows: C:\PS> $a = 1,2,3 C:\PS> $a += 2 C:\PS> $a 1 2 3 2 C:\PS> $a += "String" C:\PS> $a 1 2 3 2 String When the value of a variable is a hash table, the += operator appends the value on the right side of the operator to the hash table. However, because the only type that you can add to a hash table is another hash table, all other assignments fail. For example, the following command assigns a hash table to the $a variable. Then, it uses the += operator to append another hash table to the existing hash table, effectively adding a new key/value pair to the existing hash table. This command succeeds, as shown in the output: C:\PS> $a = @{a = 1; b = 2; c = 3} C:\PS> $a += @{mode = "write"} C:\PS> $a Name Value ---- ----- a 1 b 2 mode write c 3 The following command attempts to append an integer (1) to the hash table in the $a variable. This command fails: C:\PS> $a = @{a = 1; b = 2; c = 3} C:\PS> $a += 1 You can add another hash table only to a hash table. At line:1 char:6 + $a += <<<< 1 THE ASSIGNMENT BY SUBTRACTION OPERATOR (-=) The assignment by subtraction operator (-=) decrements the value of a variable by the value that is specified on the right side of the operator. This operator cannot be used with string variables, and it cannot be used to remove an element from a collection. The -= operator combines two operations. First, it subtracts, and then it assigns. Therefore, the following statements are equivalent: $a -= 2 $a = ($a - 2) The following example shows how to use of the -= operator to decrease the value of a variable: C:\PS> $a = 8 C:\PS> $a -= 2 C:\PS> $a 6 You can also use t...
marius050