Windows PowerShell/Expressions

< Windows PowerShell

This lesson introduces PowerShell expressions.

Objectives and Skills

After completing this lesson, you will be able to:

Readings

  1. Read Wikipedia: Expression (computer science).
  2. Read Wikipedia: Statement (computer science).
  3. Read Wikipedia: Order of operations.
  4. Read Wikipedia: Unary operation.
  5. Read Microsoft TechNet: about_Operators.

Multimedia

  1. Watch YouTube: Windows PowerShell Fundamentals Chapter 14 - More Operators.
  2. Watch YouTube: PowerShell - How To - Operators.

Examples

Arithmetic Operators

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

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

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

$a = 1

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

Comparison Operators

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

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

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

  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.
  5. Use the Discuss page to post comments and questions regarding this lesson.
  6. Review the lesson summary, key terms, review questions and assessments below.

Lesson Summary

Key Terms

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

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.

-splt and -join are _____.

-splt and -join are string operators.

Assessments

References

This article is issued from Wikiversity - version of the Tuesday, November 10, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.