Assignment Operators

The assignment operators are used in the PowerShell to assign one or more values and change or append the values to the variable. These operators can perform the numeric operations before assigning the values to the variables. The most commonly used operator is an assignment operator (=), which assigns the values to the variable.

PowerShell supports the following assignment operators:

PowerShell Assignment Operators

  1. = (Assignment operator)
  2. += (Addition Assignment operator)
  3. -= (Subtraction Assignment operator)
  4. *= (Multiplication Assignment operator)
  5. /= (Division Assignment operator)
  6. %= (Modulus Assignment operator)
  7. ++ (Increment Operator)
  8. –(Decrement Operator)

Syntax

The following statement is the syntax which describe how to use the assignment operators:

In the above statement, the assignable expression includes the variables and properties. The value can be a single value, array of values, an expression, command, or a statement.

Assignment operator (=)

The assignment operator is an assignment operator, which is used to assign one or more values (either string, integer, or an array) to the variable.

There are some following examples which describe how to use the assignment operator:

Example1: This example is used to assign the single integer value to the variable:

The second statement in this example displays the value of the variable $a as 10.

Example2: This example is used to assign more than one value (i.e., an array) to the variable. The commas separate the values in this example:

The second command in this example displays the following values of an array $k as:

10  20  30   

Example3: This example is used to assign the string to the variable:

The second command in this example displays the value of variable $s as “Windows PowerShell“.

Addition Assignment operator (+=)

The addition assignment operator is an assignment operator, which increases the value of the variable by a given value or appends the specified value to the existing value. This operator combines the two operations. First, it adds the values and then assigns to the variable.

There are some following examples which describe how to use the addition assignment operator in PowerShell:

Example1: This example is used to add the single integer value to the variable:

The last statement in this example displays the value of the variable $a as 15.

Example2: This example is used to append the string to the variable:

The last command in this example displays the value of variable $s as “Windows PowerShell“.

Example3: This example is used to append one or more than one value to an array. If we append more than one value to an array, separate them by the commas.

The last command in this example displays the following values of an array $k as:

10  20  30   40  50  

Subtraction Assignment operator (-=)

The subtraction assignment operator is an assignment operator, which decreases the value of the variable by a given value that is available on the right side of the operator. This operator combines the two operations. First, it subtracts the values, and then it assigns to the variable. It also decreases the value of an element in a numeric array.

There are some following examples which describe how to use the subtraction assignment operator in PowerShell:

Example1: This example decreases the value of the variable:

The last statement in this example displays the value of the variable $a as 40.

Example2: This example decreases the value of the second element by -20 in an array

The last command in this example displays the following values of an array $k as:

10  10  60   

Multiplication Assignment operator (*=)

The multiplication Assignment operator is an assignment operator, which multiplies the value of the variable by a given numeric value or appends the specified number of copies of the string to the variable. This operator combines the two operations. First, it multiplies the values, and then it assigns to the variable.

There are some following examples which describe how to use the multiplication assignment operator in PowerShell:

Example1: This example is used to multiply the integer value to the variable:

The last statement in this example displays the value of the variable $a as 50.

Example2: This example is used to append the string to the variable:

The last command in this example displays the value of variable $s as “WindowsWindows“.

Example2: This example multiply the value of the second element by 4 in an array.

The last command in this example displays the following values of an array $k as:

10  120  60   

Division Assignment operator (/=)

The division Assignment operator is an assignment operator, which divides the value of the variable by a given numeric value, which is specified on the right side of the operator. This operator cannot be used with string values.

This operator combines the two operations. First, it divides the values, and then it assigns to the variable.

There are some following examples which describe how to use the division assignment operator in PowerShell:

Example1: This example is used to divide the integer value of a variable:

The last statement in this example displays the value of the variable $a as 10.

Example2: This example divide the value of first element by 2 in an array

The last command in this example displays the following values of an array $k as:

50  10   80  

Modulus Assignment operator (%=)

The modulus Assignment operator is an assignment operator which is used to calculate the remainder of the division operation. This operator divides the value of a variable by the specified value given on the right side of the operator. After, it assigns the remainder to the variable.

This operator cannot be used with string values and arrays.

The following example describes how to use the modulus assignment operator in PowerShell:

This example calculates the remainder of the division operation:

The last statement in this example displays the value of the variable $a as 2.

Increment operator (++)

The increment operator is an assignment operator, which is used to increase the value of the variable by 1. When we use this operator in a simple statement, it returns no value.

Postfix Increment Operator:

The following example describe how to use the postfix increment operator:

The last two commands in this example display the value of the variable $p and $a as 5 and 6.

Prefix Increment Operator:

The following example describes how to use the postfix increment operator:

The last two commands in this example display the value of the variable both $p and $a as 6.

Decrement operator (–)

The decrement operator is an assignment operator which is used to decrease the value of the variable by 1. When we use this operator in a simple statement, it returns no value.

Postfix Decrement Operator:

The following example describes how to use the postfix increment operator:

The last two commands in this example display the value of the variable $p and $a as 5 and 4 respectively.

Prefix Decrement Operator:

The following example describes how to use the postfix increment operator:

The last two commands in this example display the value of the variable both $p and $a as 4.