PowerShell Assignment Operators

In this tutorial we will go through PowerShell Assignement Operators. These operators assign a value to multiple values to a variable. Assignment operators are also able to perform numeric operations before the assignment of a value to the variable. We will go through the list of PowerShell assignment operators and see few examples about them that will help you understand what they are able to do and their use. Below is the list of assignment operators that are supported by PowerShell as provided by Microsoft:

= : Sets the value of a variable to the specified value.

: 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.

: 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.

: 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.

: 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. %= : Divides the value of a variable by the specified value and then assigns the remainder (modulus) to the variable.

: 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.

: 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.

Variables

As per Microsoft, variables are named memory spaces that store values. In order to assign a value to a variable, we have to use = operator. When we will use assignment operator to store a value or multiple values, if the variable does not exists, it will be created. If the variable exists, the value will be replaced with the new value that you have assigned. PowerShell variables do not have any specific type. Variables are taking their data type based on the value that is stored in them. If a collection of items is stored in a variable, then the variable’s data type will become System.Object and you will be able to store any data type in it.

[adinserter name=”In Article”]

= Assignment Operator

Below you will see few ways on how you can store values in variables using the assignment operator.

Providing a number to the variable $a = 5

Providing a string to the variable $a = "Hello World"

Providing multiple values to create an array $a = "This" , "is" , "an" , "array"

Providing key/value pairs to create a hash table $a = @{1 = "This" ; 2 = "is" ; 3 = "a" ; 4 = "hash" ; 5 = "table"}

Providing a hexadecimal value $a = 0x37 (Decimal Value: 55 – The value is converted in decimal)

(Decimal Value: 55 – The value is converted in decimal) Providing exponential numbers $a = 5.55e5 (Value is 555 000)

(Value is 555 000) Providing KB, MB or GB $a = 55mb (value is converted into bytes)

(value is converted into bytes) Providing a command $a = Get-Alias

Providing an expression $a = if ($x -ge 5) { "Hello" } else { "World" }

The screenshots that you will see below are showing the output of $a variable within the console based on above examples

[adinserter name=”In Article”]

+= Addition and Assignment Operator

By using this operator we are able perform operations on integers, strings, arrays and hash tables. The action that the operator will take depends on the type of the variable and the type of the input. Let’s see below few examples and try to explain them.

In general the operator first performs the addition and then assign the value to the variable. The two lines below will result the same:

$a += 5 $a = ($a + 5)

If the variable is empty the operator will assign the value to the variable. If the variable is an integer, the operator will increase the number of the variable by the specified number.

Code:

$a += 5 $a $a += 10 $a

Output:

We are able to use addition with assignment operator in order to combine strings and here-strings. The examples below show you how this can be performed.

Code:

$a = "Hello" $a += " World" $a $a = @" Hello World. This is a beautiful day "@ $a += @" The sun is shining "@ $a

Output:

When our variable is an array, by using the assignment operator with addition we can add items in our array, either by adding one by one or adding an array in the array. When we have an array, we are able to perform changes only to a specific item of the array. We are able to perform the same changes as I have shown above. Let’s see below our examples to understand better.

Code:

$a = 5,10,15,20,25 $a += 30 $a $a += 35,40 $a $a[6] += 7 $a

Output:

When we want to use the operator for a hast table, we are only allowed to add another hash table. If you try to add any other type in the hash table you will receive error. The below example shows have you can use the operator with a hast table. Note also that you are not able to add key that already exists in the hash table.

Code:

$a = @{1 = "This" ; 2 = "is" ; 3 = "a" ; 4 = "hash" ; 5 = "table."} $a += @{6 = "The" ; 7 = "sun" ; 8 = "is" ; 9 = "shining."} $a

Output:

The operator processes the changes based on the type of the variable. If the variable is System.Integer the it will perform the changes on the value of the variable as numeric values. If the variable type is System.String it will perform the changes as string. You can enforce a variable to be string even if it contains a numeric value. Check the below examples to understand how the operator will react on this.

Code:

$a = "String" $a.GetType().FullName $a += 5 $a $a = 55 $a.GetType().FullName $a += "5" $a $a = "55" $a.GetType().FullName $a += 5 $a [string]$a = 55 $a.GetType().FullName $a += 5 $a

Output:

[adinserter name=”In Article”]

-= Subtraction and Assignment Operator

Unlike Addition and Assignment Operator, this operator is only able to work with integers and it is not able to remove any items from arrays or hash tables. In general, the proccess is the same with integers. The below lines are performing the same task:

$a -= 5 $a = ($a - 5)

The operator will subtract the specified integer from the variable. In case of arrays we are able to use the operator in order to modify an item in the array by specifying the array index. The below examples will help you understand.

Code:

$a = 15 $a -= 5 $a $a = 5,10,15,20,25 $a[4] -= 4 $a

Output:

*= Multiplication and Assignment Operator

This operator is able to work with numeric values and strings. In the case that we have a numeric value, the operator will multiply the current value of a variable by the times that you will specify and the assign the new value to the variable. When the variable contains string, the operator will multiply the string by the times that you will specify, combine the string and then assign it to the variable. In case we have an array, we are only able to perform operations on items within the array by specifying the index number of the item. The below examples will help you understand better the use of this operator.

Code:

$a = 10 $a *= 5 $a $a = 5,10,15,20,25 $a[3] *= 5 $a $a = "String" $a *= 5 $a

Output:

[adinserter name=”In Article”]

/= Division and Assignment Operator

This operator is only able to work numeric values in the same concept as subtraction and assignment operators. The examples below will show you the use of this operator, with a numeric value in our variable and also an array variable performing changes to an items within the array.

Code:

$a = 10 $a /= 5 $a $a = 5,10,15,20,25 $a[3] /= 5 $a

Output:

%= Modulus and Assignment Operator

This operator is only able to work with numeric values, either as item within an array or not. It will calculate the modulus and then assign that value to our variable o item within the array. The examples below will show you the use of it.

Code:

$a = 15 $a %= 4 $a $a = 5,10,15,20,25 $a[3] %= 3 $a

Output:

[adinserter name=”In Article”]

++ Increment Operator

Increments Operator is able to work only with numeric values. The operator will increment the current value of a variable by 1. This can be applied also in arrays to increase the value of an item in an array by 1. There operator will not provide you any output of the new value. In order to get the output of the operator you need to enclose it in parentheses ( ) .

Code:

$a = 5 ++$a $a (++$a)

Output:

The position of the increment operator plays a significant role on the result. If the the operator is placed before the variable, the operator is processed before the variable value. If it is placed after the variable the operator is processed after the variable value. It does not matter if you enclose the operator with the variable within parentheses ( ) , the outcome is the same. Check the next examples to understand the difference.

Code:

$a = 5 ++$a $a (++$a) $a = 5 $b = ++$a $a $b $a = 5 $b = $a++ $a $b $a = 5 $b = (++$a) $a $b $a = 5 $b = ($a++) $a $b

Output:

-- Decrement Operator

The decrement operator decreases the numeric value of a variable by 1. It works exactly the same way as the increment operator. It works only on numeric values either in array or not.

I hope the tutorial about PowerShell Assignment Operators is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

[adinserter name=”Matched-Content”]