Assignment Operators


PowerShell uses assignment operators to set values to variables. We've been using the equal sign, but there are many other operators as well. Table 6.3 lists PowerShell assignment operators:

image from book
Table 6.3: PowerShell Assignment Operators
Open table as spreadsheet

Operator

Description

=

Sets a value of a variable to the specified value.

+=

Increases the value of a variable by the specified value or appends to the existing value.

-=

Decreases the value of a variable by the specified value.

*=

Multiplies the value of a variable by the specified value or appends to the existing value.

/=

Divides the value of a variable by the specified value.

%=

Divides the value of a variable by the specified value and assigns the remainder (modulus) to the variable.

image from book

In addition to the traditional uses of =, in PowerShell this operator has a few extra bells and whistles that might be of interest to you. First, when you assign a hexadecimal value to a variable, it is stored as its decimal equivalent:

 PS C:\> $var=0x10 PS C:\> $var 16 PS C:\> 

You can also use a type of shorthand to assign a variable a multiple byte value. By using kb, mb, and gb, which are known as numeric constants, you store actual kilobyte, megabyte, and gigabyte values respectively:

 PS C:\> $var=10KB PS C:\> $var 10240 PS C:\> $var=2MB PS C:\> $var 2097152 PS C:\> $var=.75GB PS C:\> $var 805306368 PS C:\> 

In the first example we set $var to 10KB or 10 kilobytes. Displaying the contents of $var shows the actual byte value of 10 kilobytes. We repeat the process by setting $var to 2 megabytes and then .75 gigabytes. In each example we display the value of $var. By the way, there is no numeric constant for a terabyte.

The += operator increases the value of a given variable by a specified amount:

 PS C:\> $var=7 PS C:\> $var 7 PS C:\> $var+=3 PS C:\> $var 10 PS C:\> 

The variable $var begins with a value of 7. We then use += to increment it by 3, which changes the value of $var to 10.

The -= operator decreases the value of a given variable by a specified amount. Let's continue with the previous example:

 PS C:\> $var-=3 PS C:\> $var 7 PS C:\> 

$var starts out with a value of 10. Using the -= operator we decrease its value by 3, which returns us to 7.

What if we want to multiply a variable value by a specific number? This calls for the *= operator. Let's continue with the same $var that currently has a value of 7:

 PS C:\> $var*=3 PS C:\> $var 21 PS C:\> 

We can also divide by use the /= operator:

 PS C:\> $var/=7 PS C:\> $var 3 PS C:\> 

Finally, we can use %= to divide the variable value by the assigned value and return the modulus or remainder:

 PS C:\> $var=9 PS C:\> $var%=4 PS C:\> $var 1 PS C:\> 

In this example we start with a $var value of 9. Using the modulus assignment operator with a value of 4 means we're diving 9 into 4. The remainder value is then assigned to $var, which in this example is 1.

You need to be careful with assignment operators and variable values. Remember PowerShell does a pretty good job at deciding if what you typed is a number or a string. If you put something in quotes PowerShell treats it as a string. If you're not careful, you can get some odd results:

 PS C:\> $var="3" PS C:\> $var+=7 PS C:\> $var 37 PS C:\> 

In this example we think we set $var to 3 and increased it by 7 using the += operator. However, "3" is a string, so the += operator simply concatenates instead of adds, which is why we end up with a $var value of 37. If you are ever unsure about what type of object you're working with, you can use GetType():

 PS C:\> $var.gettype() IsPublic IsSerial Name                                   BaseType -------- -------- ----                                   -------- True     True     String                               System.Object PS C:\> 

One final comment on assignment operators: It is possible to assign values to multiple variables with a single statement:

 PS C:\> $varA,$varB,$varC="Apple",3.1416,"Windows" PS C:\> get-variable var? Name                           Value ----                           ----- varC                           Windows varB                           3.1416 varA                           Apple PS C:\> 

The assigned values are set to their respective variables. If you have more values than variables, then the extra values are assigned to the last variable:

 PS C:\> $varA,$varB,$varC="Apple",3.1416,"Windows","Linux" PS C:\> get-variable var? Name                           Value ----                           ----- varC                           {Windows, Linux} varB                           3.1416 varA                           Apple PS C:\> 

Our recommendation is to be careful with this type of statement since you can end up with unintentional variable values. PowerShell will wait, so set and modify variables one at a time.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net