Basic Windows PowerShell 1.0 Operators

From Techotopia
Revision as of 18:43, 21 November 2008 by Neil (Talk | contribs) (New page: 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...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 at briefly 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:

OperatorDescriptionExample
+=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 /= yDivide value of variable by right hand operand and place result in variable.$x /= 10
x %= yDivide value of variable by right hand operand and place the remainder in variable.$x % 10

C# Arithmetic Operators

C# 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). For example:

int x = -10; // Unary - operator used to assign -10 to a variable named x

x = y - z; // Subtraction operator. Subtracts z from y

The following table lists the primary C# arithmetic operators:

OperatorDescription
-(unary)Negates the value of a variable or expression
*Multiplication
/Division
+Addition
-Subtraction
%Modulo

Note that multiple operators may be used in a single expression. For example:

x = y * 10 + z - 5 / 4;

Whilst the above code is perfectly valid it is important to be aware that C# does not evaluate the expression from left to right or right to left, but rather in an order specified by the precedence of the various operators. Operator precedence is an important topic to understand since it impacts the result of a calculation and will be covered in detail the next section.