Chapter 6: Operators


Operators in PowerShell are language elements that let you do things like add or compare variables and values. In this chapter we'll introduce you to a variety of operators you will use over and over.

Arithmetic Operators

Arithmetic operators allow you to perform mathematical calculations with numeric values from variables or parameters. With these operators you can add, subtract, multiply and divide. You can then pass the result as a parameter to another process or cmdlet. Table 6.1 lists these operators.

image from book
Table 6-1: PowerShell Arithmetic Operators
Open table as spreadsheet

Operator

Description

Example

+

Add two values together.

PS C:\> 5+4

9

-

Subtract one value from another.

PS C:\> 134-90

44

-

Change a value to a negative number.

PS C:\>-6

-6

*

Multiply two values together.

PS C:\> 3*4.5

13.5

/

Divide one value by another.

PS C:\> 6/4

1.5

%

Return the remainder from a division. This is also known as the modulus.

PS C:\Temp> 6%4

2

image from book

As you see from the examples in the table, results are not limited to integer values. In fact, you can obtain some pretty detailed results:

 PS C:\> 3.1416*3 9.4248 PS C:\> 3.1416/12345 0.000254483596597813 PS C:\> 

Extra Credit

In PowerShell you also have access to the .NET Framework Math class. If you need more sophisticated mathematical operations such as square root or raising a number to a power, those methods can be invoked like this:

 PS C:\> [system.math]::pow(2,16) 65536 PS C:\> [system.math]::sqrt(5) 2.23606797749979 

See http://msdn2.microsoft.com/en-us/library/xaz41263.aspx for a full list of available methods for the Math class.

Precedence

As you probably learned in elementary school, there is an order for evaluating arithmetic operators. Suppose you have an expression like 5+1/2*3. Would you be surprised that the answer is 6.5? Take a moment and look at the order of precedence:

  1. - (for a negative number)

  2. *, /, %

  3. +, - (for subtraction)

Expressions are evaluated left to right following the order above. So when 5+1/2*3 is evaluated, 1 divided by 2 is first, which gives us .5. This value is then multiplied by 3, which equals 1.5. Next, 1.5 is added to 5 for a result of 6.5.

We use parentheses to override the precedence. In this case, (5+1)/(2*3) will equal 1. Parenthetical elements are evaluated first then the rest of the expression is evaluated. In the new example 5+1=6 and 2*3=6, then 6 divided by 6 equals 1.

Variables

Using arithmetic operators with variables is no different than using numbers, as long as the variable contains a number:

 PS C:\> $var1="Windows" PS C:\> $var2=100 PS C:\> $var1+var2 You must provide a value expression on the right-hand side of the '+' operator. At line:1 char:7 + $var1+v <<<< ar2 PS C:\> 

In this example we set two variables, but one of them is a string. When we try to use the + operator, PowerShell throws an error. Here is a valid example:

 PS C:\> $var1=5 PS C:\> $var2=1 PS C:\> $var3=2 PS C:\> $var4=3 PS C:\> ($var1+$var2)/($var3*$var4) 1 PS C:\> 

This is essentially the same example we used earlier, except that we substituted variables.

Here's a more practical example:

 PS C:\> $proc=Get-process PS C:\> $total=0 PS C:\> foreach ($p in $proc) { >> $total=$total+$p.workingset >> } >> PS C:\> $total 584568832 PS C:\> 

This example gets the total number of bytes for all running processes allocated to processes working set. First we send the output of Get- Process to a variable and initialize the total variable to zero. We then use a simple foreach loop and add the working set size of each process object to the running total. Finally, we display the value of $total.

Since the value of $total is in bytes, we might want to display it in KB or MB:

 PS C:\> $total/1024 570868 PS C:\> $total/(1024*1024) 557.48828125 PS C:\> 

One word of caution when trying to mix numbers and text such as when using Write-Host. This cmdlet will expand variables but not perform any arithmetic operations:

 PS C:\> write-host "Total working set size="$total/1024 "KB" Total working set size= 584568832/1024 KB PS C:\> 

That's not really what we're after. By enclosing $total/1024 in parenthesis, the expression is evaluated and written to the screen as expected:

 PS C:\> write-host "Total working set size="($total/1024) "KB" Total working set size= 570868 KB PS C:\> write-host "Total working set size="($total/(1024*1024)) "MB" Total working set size= 557.48828125 MB PS C:\> 

Notice in the second example we're using nested parentheses. The inner most parentheses are evaluated first and then the expression enclosed in the outer parentheses.

Unary Operators

A subtype of the arithmetic operator is a unary operator, which is used to increment or decrement a variable's value. PowerShell uses ++ to increase a value by one and -- to decrease it:

 PS C:\> $var=10 PS C:\> $var++ PS C:\> $var 11 PS C:\> $var-- PS C:\> $var 10 PS C:\> 

$var starts with a value of 10. Using the ++ operator doesn't appear to do anything, but it actually increased the value of $var by 1. Using the - operator decreases the value by 1.



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