(10) PowerShell assignment operator (i)

Source: Internet
Author: User
Tags at sign

The previous section describes the built-in variables in PowerShell, and the details are poked here.


This section describes the assignment operators in PowerShell , which assign one or more values to a variable. The assignment operator can perform a numeric operation on a value before assigning it.


PowerShell supports the following assignment operators.

Operator Description Note
= Sets the value of the variable to the specified value.
+= Increase the value of the variable by the specified value, or append the specified value to the existing value.
-= Reduces the value of the variable by the specified value.
*= Multiplies the value of a variable by the specified value, or appends the specified value to an existing value.
/= Divides the value of a variable by the specified value.
%=
Divide the value of the variable by the specified value, and then assign the remainder (modulo) to the variable.
++ Increases the value of a variable, an assignable property, or an array element by 1. Unary operators, in the form of prefixes and suffixes, and assignable expressions must be numbers or can be converted to numbers.
--
Reduces the value of a variable, an assignable property, or an array element by 1. Unary operators, in the form of prefixes and suffixes, and assignable expressions must be numbers or can be converted to numbers.

    1. Syntax for assignment operators

The syntax for the assignment operator is as follows:

< assignable expressions >< assignment operators (what is listed in the table above) >< values;

Assignable expressions include variables and attributes, which can be a single value, an array of values, a command, an expression, or a statement, such as

PS c:\> $a =1ps c:\> $pro =get-processps c:\> $a = "Apple", "orange", "lemon", "Grape"

2. Assignment operator (=)

Variables are memory spaces that store values, and you can store values in variables using assignment operators, which can replace existing values of variables or append values to existing values. For example, the following statement assigns the string "Powershell" to the variable $mystr

PS c:\> $myStr = "Powershell"

When you assign a value to a variable in PowerShell, the variable is created if it does not exist. For example, the first of the two assignment statements below creates a $a variable and assigns the value 1 to the $a. The second assignment statement assigns the value 3 to the $a. The first statement creates a new variable. The second statement changes only the value of the variable:

PS c:\> $a =1ps c:\> $a =3

A variable in PowerShell does not have a specific data type unless it is converted . If a variable contains only one object, the variable takes the object's data type. If the variable contains a collection of objects, the variable is the System.Object data type. Therefore, you can assign any type of object to the collection. For example, the following statement can add a process object, a service object, a string, and an integer to a variable without generating an error:

PS c:\> $a =get-processps c:\> $a +=get-serviceps c:\> $a + = "My string" PS c:\> $a +=13

the assignment operator (=) has a lower precedence than the pipe operator (|), so you do not need parentheses to assign the result of the command pipeline to a variable. For example, the following command sorts the processes on the computer and then assigns the sorted process to the $a variable:

PS c:\> $a =get-process | Sort-object-property Name

You can also assign the value created by the statement to a variable, as shown in the following statement

PS c:\> $a =if ($b-lt 0) {0} else {$b}

-LT is the abbreviation for less THen, which is equivalent to <= in a high-level language, as described in the PowerShell comparison operator. The above statement means that if the value of $b is less than 0 o'clock, assign 0 to $a variable. If the value of $b is not less than 0, the $b value is assigned to the $a.

If you want to assign a hash table to a variable , use standard hash table notation in PowerShell. An at sign (@) after which you type a semicolon (;) A key/value pair that is delimited, enclosed in curly braces ({}). For example, the following statement assigns a hash table to a $a variable:

PS c:\> [email protected]{one=1; two=2;three=3}

If you want to assign a hexadecimal value to a variable, precede the value with "0x". PowerShell converts the hexadecimal value (0x10) to a decimal value (16 in this case), and then assigns the value to the $a variable. For example, the following statement assigns the value 0x10 to the variable $a:

PS c:\> $a =0x10ps c:\> $a 16

If you want to assign an exponential value to a variable, you need to enter the root number, the letter "E", and a number that represents a multiple of 10. For example, to assign a power value of 3.1415 to a $a variable, enter:

PS c:\> $a =3.1415e2ps c:\> $a 314.15

You can also convert kilobytes (KB), megabytes (MB), and gigabytes (GB) to bytes in PowerShell. For example, to assign a value of 10,000 bytes to a $a variable, enter:

PS c:\> $a =10kb

3. Addition Assignment operator (+ =)

The addition assignment operator (+ =) Either increases the value of the variable or appends the specified value to the existing value. The action depends on whether the variable is a number or a string type, and whether the variable contains a single value (scalar) or multiple values (collections). The + = operator combines two operations. The addition is performed first, and then the value is assigned. Therefore, the following statements are equivalent.

PS c:\> $a +=2ps c:\> $a = ($a +2)

If the variable contains a single numeric value , the + = operator causes the existing value to increase the number to the right of the operator. Assigns the operator the resulting value to the variable. For example:

PS c:\> $a =5ps c:\> $a +=3ps c:\> $a 8

if the value of the variable is a string , append the value to the right of the operator to the string, as follows:

PS c:\> $a = "Windows" PS c:\> $a + = "PowerShell" PS c:\> $aWindows PowerShell

if the value of the variable is an array , the + = operator appends the value to the right of the array. Unless you explicitly specify the type of the array by conversion, you can append values of any type to the array, as follows:

PS c:\> $a =1,2,3ps c:\> $a +=4ps c:\> $a 1234PS c:\> $a + = "string" PS c:\> $a 1234string

If the value of the variable is a hash table, the + = operator appends the value to the right of the hash table. However, because you can only add another hash table to the Hashtable, all other assignments will fail .

For example, the following command assigns a hash table to a $a variable. The command then appends another hash table to the existing hash table using the + = operator, which is actually adding a new key/value pair to the existing hash table. This command succeeds with the output as follows:

Ps c:\> [email protected]{a=1;b=2;c=3}ps c:\> [email protected]{four= "4"} ps c:\>  $aName                             value----                             -----a                                1b                               2four                             4c                               3

The following command attempts to append an integer (1) to the hash table in the $a variable. This command will fail:

PS c:\> [email protected]{a=1; b=2; c=3}ps c:\> $a +1 You can only add another hash table to the Hashtable. Location Line: 1 characters: 4+ $a + <<<< 1 + categoryinfo:invalidoperation: (1:int32) [], RuntimeException + Fullyqualifiederrorid:addhashtabletononhashtable

4. Subtraction assignment operator (-=)

The subtraction assignment operator (-=) reduces the value of a variable by the value specified on the right side of the operator. This operator cannot be used with string variables, nor can it be used to remove elements from the collection . The-= operator combines two operations. The subtraction is performed first, and then the value is assigned. Therefore, the following statements are equivalent:

PS c:\> $a-=2ps c:\> $a = ($a-2)

The following statement uses the-= operator to reduce the value of a variable:

PS c:\> $a =9ps c:\> $a-=4ps c:\> $a 5

You can also use the-= assignment operator to reduce the value of a member of a numeric array. Specify the index of the array element you want to change. In the following example, the value of the third element (element 2) in the array is reduced by 1:

PS c:\> $a =1,3,5ps c:\> $a [1]-=1ps c:\> $a 125

You cannot use the-= operator to delete a variable's value. To remove all values that are assigned to a variable, use the Clear-item or Clear-variable cmdlet to assign the value $null or "" to the variable.

PS c:\> $a =8ps c:\> $a = $nullPS c:\> $a

to remove a specific value from an array, use array notation to assign the value $null to that particular item . For example, the following statement removes the second value from the array (index position 1):

PS c:\> $a =1,2,4ps c:\> $a 124PS c:\> $a [1]= $nullPS c:\> $a 14

to delete a variable, use the Remove-variable cmdlet. You can use this method when a variable has been explicitly converted to a specific data type, and you need a non-typed variable. The following command removes the $a variable:

PS c:\> $a =5ps c:\> remove-variable APS c:\> $a

It is important to note that when using the remove-variable command, there is no dollar sign ($) before the variable is used for the command name.


This article from "Flower Blossom Fall" blog, declined reprint!

(10) PowerShell assignment operator (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.