Powershell – Assignment Operators

Powershell – Assignment Operators

This has indeed been a rather queer year. However, every event has its own pros and cons. This period can be converted to one of learning and knowledge accumulation. Hence, I have started this journey of exploring the vast expanses of this world we call the “PowerShell”.

On to the topic for the day, assignment operators. Assignment operators as the name suggests are used to assign values to variables or arrays in Powershell. These operators retrieve, update and reassign variable values in one step.

Operator SymbolName
=Assignment
+=Addition assignment
-=Subtraction assignment
*=Multiplication assignment
/=Division assignment
%= Modulus assignment
++ Increment Operator
Decrement Operator

These operators are used to assign values to variables. Their usage is feasible in simple variable numeric operations and in complex scripts to act as counters.

These can be used in multiple combinations to perform actions before a value is assigned to a variable.

Syntax: <assignable-expression> <assignment-operator> <value>

$var = 1,2,33,44,87,98,09

Assignment expression – These can be variables.

Assignment operator – This represents the actual operator =, += etc.

Value – This can be a single value, multiple values, array, a command, an expression or even a statement.

Increment and Decrement Operators:

Syntax: <assignable-expression><operator> <operator><assignable-expression>

The ‘assignable expression’ must be a number, or it must be convertible to a number.

$var += 7

$var -+ 10

The major point here is that utilizing the increment or decrement operators effectively ensures that we not only add or subtract the actual variable value but also save it to the variable memory location.

Examples:

# To Assign Value

$a = 100

#Addition assignment operator

$a += 1

Equivalent expression: $a = $a + 1

#ex2

#connect to an Office 365 tenant in Powershell and then run this command

#Here we see the addition assignment operator being used to add a string to an array.

$mail = Get-Mailbox

$mail += “vignesh@technicalmusings.com”

#ex3

# The addition assignment operator being used to add a number to an array.

$a = 100,252,900

$a += 2

$a

Output :

100

252

900

2

# Subtraction assignment

$a -= 10

Equivalent Expression: $a = $a – 10

#ex2

# The Subtraction assignment operator being used to deduct the value from index number 4 in the array

$a = 1,4,7,10,14,18,22

$a[4] -= 4

$a

Output :

1

4

7

10

10

18

22

# Multiplication Assignment

$a *= 2

Equivalent Expression: $a = $a * 2

#ex2

#Strings can also be manipulated using this operator

$a = ‘I love PowerShell!’

$a *= 3

$a

Output:

I love PowerShell!

I love PowerShell!

I love PowerShell!

# Division assignment

# Will show the quotient as the output

$a /= 9

Equivalent Expression: $a = $a / 9

#ex2

$a = 100,50,25,12.5

$a[2] /= 5

$a

Output:

100

50

5

12.5

# Modulus assignment

# Will display the remainder

$a = 105

$a %= 10

$a

Output:

5

# Increment Operator

#ex1

$a = 100

$a++

$a

Output:

101

#ex2

#The increment operator can have the ‘+’ symbols before the variable as well.

$a = 100

++$a

$a

Output:

101

# Decrement Operator

#ex1

$a = 100

$a–

$a

Output:

99

#ex2

$a = 100

–$a

$a

Output:

99

#Enforcing Variables

#ex1

#The gettype() method can be used to find the type of a variable as seen here:

$a = 100

$a.GetType().FullName

#ex2

$a = ‘Hello World!’

$a.GetType().FullName

#ex3

#Enforcing the variable to have the type as ‘integer’.

[int]$b = 10

$b

$b.GetType().FullName

#ex4

#Enforcing the variable to have the type as ‘string’.

[string]$c = “SHellPower”

$c

$c.GetType().FullName