# PowerShell/Expressions

This lesson introduces PowerShell expressions.

## Objectives and Skills

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.

## Examples

#### Arithmetic Operators

Arithmetic operators calculate values.

```\$a = 3
\$b = 2

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

#### Assignment Operators

Assignment operators assign calculated values to variables.

```\$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

Unary operators increment or decrement a single variable by one.

```\$a = 1

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

#### Comparison Operators

Comparison operators compare values and test conditions.

```\$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

Logical operators compare complex conditions.

```\$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

String operators split, join, and concatenate substrings.

```\$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

1. 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.
2. 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.
3. 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.
4. 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

• 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.
• A statement is the smallest standalone element of an imperative programming language which expresses some action to be carried out.
• 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.
• 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.
• The order of operations is exponents and roots, followed by multiplication and division, followed by addition and subtraction.
• Parentheses are used to explicitly denote precedence by grouping parts of an expression that should be evaluated first.
• The mnemonic PEMDAS may be used to recall the order of operations of Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.
• A unary operation is an operation with only one operand.
• Arithmetic operators calculate values.
• Assignment operators assign calculated values to variables.
• Unary operators increment or decrement a single variable by one.
• Comparison operators compare values and test conditions.
• Logical operators compare complex conditions.
• String operators split, join, and concatenate substrings.
• +, -, *, /, and % are the addition subtraction, multiplication, division, and modulus (remainder) operators.
• =, +=, -=, *=, /=, and %= are the assignment operators.
• ++ and -- are the unary increment and decrement operators.
• -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.
• -and, -or, -xor, and -not or ! are the and, or, exclusive or, and negation logical operators.
• -split and -join are the string operators.

## Key Terms

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

## Review Questions

Click on a question to see the answer.
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.