# PowerShell/Expressions

Jump to navigation
Jump to search

This lesson introduces PowerShell expressions.

## Objectives and Skills[edit]

After completing this lesson, you will be able to:

- Describe the operators used in PowerShell scripts.
- Explain order of operation.
- Create PowerShell scripts to perform calculations based on user input.

## Readings[edit]

- Wikipedia: Expression (computer science)
- Wikipedia: Statement (computer science)
- Wikipedia: Order of operations
- Wikipedia: Unary operation
- Microsoft TechNet: about_Operators

## Multimedia[edit]

- YouTube: Windows PowerShell Fundamentals Chapter 14 - More Operators
- YouTube: PowerShell - How To - Operators

## Examples[edit]

#### Arithmetic Operators[edit]

Arithmetic operators calculate values.^{[1]}

```
$a = 3
$b = 2
$a + $b # 5
$a - $b # 1
$a * $b # 6
$a / $b # 1.5
$a % $b # 1
-$a # -3
```

#### Assignment Operators[edit]

Assignment operators assign calculated values to variables.^{[2]}

```
$a = 3
$b = 2
$a += $b # a = 5
$a -= $b # a = 3
$a *= $b # a = 6
$a /= $b # a = 3
$a %= $b # a = 1
```

#### Unary Operators[edit]

Unary operators increment or decrement a single variable by one.^{[3]}

```
$a = 1
$a++ # a = 2
$a-- # a = 1
```

#### Comparison Operators[edit]

Comparison operators compare values and test conditions.^{[4]}

```
$a = 3
$b = 2
$a -eq $b # False
$a -ne $b # True
$a -lt $b # False
$a -gt $b # True
$a -le $b # False
$a -ge $b # True
```

#### Logical Operators[edit]

Logical operators compare complex conditions.^{[5]}

```
$a = 3
$b = 2
$a -lt $b -and $b -lt $a # False
$a -lt $b -or $b -lt $a # True
$a -lt $b # False
-not ($a -lt $b) # True
```

#### String Operators[edit]

String operators split, join, and concatenate substrings.^{[6]}

```
$a = 'Cat,Dog,Fish,Hamster'
$a -split ',' # Cat
# Dog
# Fish
# Hamster
$b = @('Cat','Dog','Fish','Hamster')
$b -join ',' # Cat,Dog,Fish,Hamster
'Cat' + 'Dog' + 'Fish' + 'Hamster' # CatDogFishHamster
```

## Activities[edit]

- Review Microsoft TechNet: about_Operators. Experiment with different arithmetic operators to ensure you understand how they work. Then review Microsoft TechNet: about_Operator_Precedence and MathsIsFun: Order of Operations. Create a script that demonstrates the order of operations for PowerShell operators.
- Create a script that asks the user how old they are in years, and then calculate and display their approximate age in months, days, hours, and seconds.
- Review MathsIsFun: Conversion of Temperature. Create a script that asks the user for a Fahrenheit temperature and then calculate and display the corresponding Celsius temperature or ask the user for a Celsius temperature and then calculate and display the corresponding Fahrenheit temperature.
- Review MathsIsFun: Area of Plane Shapes. Create a script that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes.

## Lesson Summary[edit]

- An expression is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces another value.
^{[7]} - A statement is the smallest standalone element of an imperative programming language which expresses some action to be carried out.
^{[8]} - In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all.
^{[9]} - The order of operations (sometimes called operator precedence) is a rule used to clarify which procedures should be performed first in a given mathematical expression.
^{[10]} - The order of operations is exponents and roots, followed by multiplication and division, followed by addition and subtraction.
^{[11]} - Parentheses are used to explicitly denote precedence by grouping parts of an expression that should be evaluated first.
^{[12]} - The mnemonic PEMDAS may be used to recall the order of operations of Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.
^{[13]} - A unary operation is an operation with only one operand.
^{[14]} - Arithmetic operators calculate values.
^{[15]} - Assignment operators assign calculated values to variables.
^{[16]} - Unary operators increment or decrement a single variable by one.
^{[17]} - Comparison operators compare values and test conditions.
^{[18]} - Logical operators compare complex conditions.
^{[19]} - String operators split, join, and concatenate substrings.
^{[20]} - +, -, *, /, and % are the addition subtraction, multiplication, division, and modulus (remainder) operators.
^{[21]} - =, +=, -=, *=, /=, and %= are the assignment operators.
^{[22]} - ++ and -- are the unary increment and decrement operators.
^{[23]} - -eq, -ne, -gt, -lt, -le, and -ge are the equal, not equal, greater than, less than, less than or equal, and greater than or equal comparison operators.
^{[24]} - -and, -or, -xor, and -not or ! are the and, or, exclusive or, and negation logical operators.
^{[25]} - -split and -join are the string operators.
^{[26]}

## Key Terms[edit]

- identifier
- A name that identifies (that is, labels the identity of) either a unique object or a unique class of objects.
^{[27]} - modulo (sometimes called modulus)
- The operation that finds the remainder of division of one number by another.
^{[28]} - reserved word
- A word that cannot be used as an identifier, such as the name of a variable, function, or label.
^{[29]}

## Review Questions[edit]

1.
An expression is _____.

An expression is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces another value.

2.
A statement is _____.

A statement is the smallest standalone element of an imperative programming language which expresses some action to be carried out.

3.
In most languages, statements contrast with expressions in that statements _____, while expressions _____.

In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all.

4.
The order of operations (sometimes called operator precedence) is a rule used to _____.

The order of operations (sometimes called operator precedence) is a rule used to clarify which procedures should be performed first in a given mathematical expression.

5.
The order of operations is _____.

The order of operations is exponents and roots, followed by multiplication and division, followed by addition and subtraction.

6.
Parentheses are used to _____.

Parentheses are used to explicitly denote precedence by grouping parts of an expression that should be evaluated first.

7.
The mnemonic PEMDAS may be used to _____.

The mnemonic PEMDAS may be used to recall the order of operations of Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.

8.
A unary operation is _____.

A unary operation is an operation with only one operand.

9.
Arithmetic operators _____.

Arithmetic operators calculate values.

10.
Assignment operators _____.

Assignment operators assign calculated values to variables.

11.
Unary operators _____.

Unary operators increment or decrement a single variable by one.

12.
Comparison operators _____.

Comparison operators compare values and test conditions.

13.
Logical operators _____.

Logical operators compare complex conditions.

14.
String operators _____.

String operators split, join, and concatenate substrings.

15.
+, -, *, /, and % are _____.

+, -, *, /, and % are the addition subtraction, multiplication, division, and modulus (remainder) operators.

17.
++ and -- are _____.

++ and -- are the unary increment and decrement operators.

18.
-eq, -ne, -gt, -lt, -le, and -ge are _____.

-eq, -ne, -gt, -lt, -le, and -ge are the equal, not equal, greater than, less than, less than or equal, and greater than or equal comparison operators.

19.
-and, -or, -xor, and -not or ! are _____.

-and, -or, -xor, and -not or ! are the and, or, exclusive or, and negation logical operators.

20.
-split and -join are _____.

-split and -join are string operators.

## Assessments[edit]

- Flashcards: Quizlet: Windows PowerShell - Expressions
- Quiz: Quizlet: Windows PowerShell - Expressions

## See Also[edit]

## References[edit]

- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Wikipedia: Expression (computer science)
- ↑ Wikipedia: Statement (computer science)
- ↑ Wikipedia: Statement (computer science)
- ↑ Wikipedia: Order of operations
- ↑ Wikipedia: Order of operations
- ↑ Wikipedia: Order of operations
- ↑ Wikipedia: Order of operations
- ↑ Wikipedia: Unary operation
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Microsoft TechNet: about_Operators
- ↑ Wikipedia: Identifier
- ↑ Wikipedia: Modulo operation
- ↑ Wikipedia: Reserved word