CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Assignment operators

Basic Assignment operator, equal (=) sign is used to assign a value from right side value to it’s left side operand. PowerShell provides more such operators to assign the values from r-value to l-value. We are going to discuss about these operators through this Article.

Most important thing you need to remember is, all these operators expects l-value and r-value. Left side of the assignment operator is l-value and right side of the operator is the r-value. r-value will be a constant or an expression or a property or a variable. But l-value, must be either a variable or a property of an object. If you give a constant as a l-value, PowerShell will throw the below Error message; this condition is applicable to all the Assignment operators;

PS C:\> 1 = ( 2 + 3 )
At line:1 char:1
+ 1 = ( 2 + 3 )
+ ~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidLeftHandSide

Basic Assignment operator, equal (=) sign

Most of us knew that, the basic assignment operator is used to assign the values to the variables defined in the Programming / Scripting languages.

PS C:\> $v = 100

From above, the value 100 is assigned to the variable $v , using the assignment operator.

+= operator and ++ operator

These operators are used to add the specified value and assign the result to the operand, in the left side. += will add the provided value to the left side operand; where as ++ operator will increment the operand value by 1.

Here are some examples;

PS C:\> $v = 110
PS C:\> $v += 20
PS C:\> $v
130
PS C:\> $v++
131
PS C:\> ++$v
132

Observe that, you can use ++ operator; before or after the operand. Both versions, will increment the operand value by 1. ++ operator works only on numbers. Whereas += can also be used to concatenate the strings, arrays and hash tables.

-= operator and -- operator

Similar to above operators; these operators will decrements the operand value. -= will decrements by specified value and -- will decrements the value by 1.

Both these operators can be used only on numbers.

*= operator

This operator is used to multiply the value of the operand with the specified value. This operator can be used on numbers, strings and arrays. Yes, this can be used on strings and arrays too.

On numbers, it multiplies the operand value with the specified value. On strings and arrays, it appends the operand value multiple times.

/= operator and %= operator

These operators are used to divide the operand value with the specified value. %= assigns the remainder or modulus value; whereas /= assigns the result of the division of the operation.

[..] David

PowerShell – Assignment operators

One thought on “PowerShell – Assignment operators

  1. Pingback: Homepage

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top