TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Assignment Operators

PowerShell Assignment Operators with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

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:

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

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:

$a=10
$a

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:

$k=10, 20, 30
$k

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:

$s="Windows PowerShell"
$s

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:

$a=5
$a+=10
$a

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:

$s="Windows"
$s += "PowerShell"
$s

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.

$k=10, 20, 30
$k += 40, 50
$k

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:

$a=50
$a -=10
$a

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

$k=10, 30, 60
$k[1] -= 20
$k

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:

$a=5
$a *= 10
$a

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:

$s="Windows"
$s *= 2
$s

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.

$k=10, 30, 60
$k[1] *= 4
$k

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:

$a=50
$a /= 5
$a

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

$k=100, 10, 80
$k[0] /= 2
$k

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:

$a=30
$a %= 4
$a

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:

$a=5
$p = $a++
$p
$a

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:

$a=5
$p = ++$a
$p
$a

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:

$a=5
$p = $a--
$p
$a

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:

$a=5
$p = --$a
$p
$a

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






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf