Difference between revisions of "Basic Windows PowerShell 1.0 Operators"
Line 371: | Line 371: | ||
</tr> | </tr> | ||
</table> | </table> | ||
− |
Revision as of 14:52, 14 May 2009
Previous | Table of Contents | Next |
Windows PowerShell 1.0 Hashtables | Windows PowerShell 1.0 Comparison and Containment Operators |
<google>BUY_WPS</google>
In Understanding and Creating Windows PowerShell 1.0 Variables we looked at using variables in Windows PowerShell and also described the different types. Being able to create variables is only part of the story however. The next step is to learn how to use these variables in PowerShell commands and scripts. The primary method for working with the data stored in variables is in the form of expressions. In this chapter we will look in detail at PowerShell expressions and operators.
Contents | ||
What is an Expression?
The most basic expression consists of an operator, two operands and an assignment. The following is an example of an expression:
$theResult = 1 + 2
In the above example the (+) operator is used to add two operands (1 and 2) together. The assignment operator (=) subsequently assigns the result of the addition to an integer variable named $theResult. The operands could just have easily been variables or constants (or a mixture of each) instead of the actual numerical values used in the example.
In the remainder of this chapter we will look at the various types of operators available in Windows PowerShell.
Windows PowerShell Basic Assignment Operator
We have already looked briefly at the most basic of assignment operators, the = operator. This assignment operator simply assigns the result of an expression to a variable. In essence the = assignment operator takes two operands. The left hand operand is the variable to which a value is to be assigned and the right hand operand is the value to be assigned. The right hand operand is, more often than not, an expression which performs some type of arithmetic or logical evaluation. The following examples are all valid uses of the assignment operator:
$x = 10; // Assigns the value 10 to a variable named x $x = $y + $z; // Assigns the result of variable y added to variable z to variable x; $x = $y; // Assigns the value of variable y to variable x
The assignment operator may also be used to perform a cross-assignment, whereby the values of two variables are swapped:
$x,$y = $y,$x
Using a similar technique, multiple values may be assigned to multiple variables in a single line:
$x,$y,$z = 10,20,30
In addition, assignment operators may be chained together to assign the same value to multiple variables. For example, the following command assigns the value 10 to the variables $x, $y and $z:
$x = $y = $z = 10
Windows PowerShell Compound Assignment Operators
In addition to the basic assignment operator, Windows PowerShell also provides a collection of compound assignment operators which combine an arithmetic operation with the assignment. These operators are outlined in the following table: <google>ADSDAQBOX_FLOW</google>
Operator | Description | Example |
---|---|---|
+= | Add right hand operand to value of variable and place result in variable. | $x += 10 |
-= | Subtract right hand operand from value of variable and place result in variable. | $x -= 10 |
*= | Multiply value of variable by right hand operand and place result in variable. | $x *= 10 |
x /= y | Divide value of variable by right hand operand and place result in variable. | $x /= 10 |
x %= y | Divide value of variable by right hand operand and place the remainder in variable. | $x % 10 |
Windows PowerShell Arithmetic Operators
Windows PowerShell provides a range of operators for the purpose of creating mathematical expressions. These operators primarily fall into the category of binary operators in that they take two operands. The exception is the unary negative operator (-) which serves to indicate that a value is negative rather than positive. This contrasts the the subtraction operator (-) which takes two operands (i.e one value to be subtracted from another). The behavior of a number of these operators varies depending on whether the operands are numbers, strings, arrays or hashtables.
The following table lists the primary Windows PowerShell arithmetic operators:
Operator | Description | Example |
---|---|---|
-(unary) | Negates the value of a variable or expression | -10 |
* | Multiplies two values. | 10 * 20 |
/ | Divides two values | 20 / 10 |
+ | Add two values | 10 + 20 |
- | Subtracts two values | 2 - 10 |
% | Return the remainder of the division of two values | 10 % 3 |
++ | Pre and post increment of value | $val++, ++$val |
-- | Pre and post decrement of value | $val--, --$val |
Note that multiple operators may be used in a single expression. For example:
$x = $y * 10 + $z - 5 / 4;
The Addition Operator
If the left hand operand of an addition expression is a number, PowerShell will attempt to convert the right hand operand to a number (if it is not already one) and perform an arithmetic addition. If the right hand operand cannot be converted to a number, an error will be reported:
PS C:\Users\Administrator> $x = 10 + 20 # Both operands are numeric, no conversion required PS C:\Users\Administrator> $x 30 PS C:\Users\Administrator> $x = 10 + "20" # Right hand operand is a string, converted to numeric type PS C:\Users\Administrator> $x 30 PS C:\Users\Administrator> $x = 10 + "twenty" # Right hand operand is a string, conversion impossible Cannot convert value "twenty" to type "System.Int32". Error: "Input string was not in a correct for mat." At line:1 char:10 + $x = 10 + <<<< "twenty"
If both operands are strings, the addition operand concatenates the strings together:
PS C:\Users\Administrator> $string = "Hello, " + "there" PS C:\Users\Administrator> $string Hello, there
Similarly, if the operands are arrays or hashtables, the right hand operand will be appended to the end of the left hand operand.
Multiplication Operator
If the left hand operand of a muliplication expression is a number, PowerShell will attempt to convert the right hand operand to a number (if it is not already one) and perform an arithmetic addition. If the right hand operand cannot be converted to a number, an error will be reported:
PS C:\Users\Administrator> $x = 10 * 20 # Both operands are numeric, no conversion required PS C:\Users\Administrator> $x 200 PS C:\Users\Administrator> $x = 10 * "20" # Right hand operand is a string, converted to numeric type PS C:\Users\Administrator> $x 200 PS C:\Users\Administrator> $x = 10 * "twenty" # Right hand operand is a string, conversion impossible Cannot convert value "twenty" to type "System.Int32". Error: "Input string was not in a correct for mat." At line:1 char:10 + $x = 10 * <<<< "twenty"
If the left hand operand is a string and the right hand operand a number, the string will be duplicated the specified number of times:
PS C:\Users\Administrator> $string = "Hello, " * 3 PS C:\Users\Administrator> $string Hello, Hello, Hello,
Similarly, multiplication of an array causes duplication of array elements as illustrating in the following example:
PS C:\Users\Administrator> $myarray = "red","green","blue" PS C:\Users\Administrator> $myarray red green blue PS C:\Users\Administrator> $myarray = $myarray * 3 PS C:\Users\Administrator> $myarray red green blue red green blue red green blue
Division, Subtraction and Modulus Operators
Unlike the above operators, the Windows PowerShell divide, subtract and modulus operators are designed only to work with numbers. It is not, therefore, possible to divide one string by another or subtract two arrays. In terms of performing conversions, as long as one operand is a number, PowerShell will attempt convert the remaining operand to a number (assuming once again, of course, that it is not already a number). For example, the following expressions all produce the same result:
PS C:\Users\Administrator> 10 / 20 0.5 PS C:\Users\Administrator> "10" / 20 0.5 PS C:\Users\Administrator> 10 / "20" 0.5
Two operands of type string, however, will not be converted to numbers, instead producing an error message:
PS C:\Users\Administrator> "10" / "20" Method invocation failed because [System.String] doesn't contain a method named 'op_Division'. At line:1 char:7 + "10" / <<<< "20"=
PowerShell Increment and Decrement Operators
The increment and decrement operators are inherited from the C programming language and appear in many other programming and scripting languages. For those not familiar with the concept, these operators require a little explanation. Let us start by considering the following PowerShell expressions:
$x = 10 $x = $x + 1; // Increase value of variable $x by 1 $x = $x - 1; // Decrease value of variable $x by 1
These expressions increment and decrement the value of variable $x by 1. Instead of using this approach it is quicker to use the ++ and -- operators. The following examples perform exactly the same tasks as the examples above:
$x++; Increment x by 1 $x--; Decrement x by 1
These operators can be placed either before or after the variable name. If the operator is placed before the variable name, the increment or decrement is performed before any other operations are performed on the variable. For example, in the following example, $x is incremented before it is assigned to $y, leaving $y with a value of 10:
$x = 9 $y = ++$x $x 10 $y 10
In the following example, the value of $x (9) is assigned to variable $y before the decrement is performed. After the expression is evaluated, therefore, the value of $y will be 9 and the value of $x will 8.
$x = 9 $y = $x-- $x 8 $y 9
Windows PowerShell & Operator
The Windows PowerShell & operator is a little known, yet extremely useful operator. Quite simply, it allows a string to be treated as a command. This is particularly useful when creating dynamic commands within a PowerShell script. In the following example, the following commands assign the string Get-ChildItem to a variable named $mycommand:
PS C:\Users\Administrator> $mycommand="get-childitem"
Simply referencing the string does nothing more than display the value of the variable:
PS C:\Users\Administrator> $mycommand get-childitem
Prefixing the string with the & operator, however, instructs PowerShell that the variable contains a command which is to be executed:
PS C:\Users\Administrator> &$mycommand Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Administrator Mode LastWriteTime Length Name ---- ------------- ------ ---- d-r-- 11/7/2008 4:26 PM Contacts d-r-- 11/7/2008 4:26 PM Desktop d-r-- 11/7/2008 4:26 PM Documents d-r-- 11/7/2008 4:26 PM Downloads
Windows PowerShell Bitwise and Logical Operators
As with most other programming and scripting languages, Windows PowerShell supports the usual range of logical operators for performing operations such as AND, OR, XOR and NOT. The full range of logical operators is outlined in the following table:
Operator |
Description |
---|---|
-and |
Perform a logical AND of the left and right operands |
-or |
Perform a logical OR of the left and right operands |
-xor |
Perform a logical XOR of the left and right operands |
-not |
Perform a logical NOT of the left and right operands |
-band |
Perform a logical binary AND of the left and right operands |
-bor |
Perform a logical binary OR of the left and right operands |
-bxor |
Perform a logical binary XOR of the left and right operands |
-bnot |
Perform a logical binary NOT of the left and right operands |