Chapter 3. Operators and expressions

 

This chapter covers

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Pattern matching and text manipulation
  • Logical and bitwise operators

Operators, Mr. Rico! Millions of them!

Robert A. Heinlein, Starship Troopers (paraphrased)

The goal of PowerShell is to enable you to get real work done. As in any language, expressions consist of operators and objects. The operators perform their operations on objects, giving you (hopefully) useful results. This chapter covers the set of basic operators in PowerShell and how they’re used in expressions. Without operators PowerShell can’t perform comparisons, arithmetic, logical operations, or a host of other activities. The operators we’re going to cover in this chapter are shown in figure 3.1.

Figure 3.1. Broad groups of operators we’ll cover in this chapter
Note

Operators are normally classed as unary if they take a single operand and binary if they take two. The operators in this chapter are all binary. We’ll look at unary operators in chapter 4.

As you’ll see, PowerShell has lots of operators. PowerShell operators are typically more powerful than the corresponding operators in conventional languages such as C# or Java. If you invest the time to learn what the PowerShell operators are and how they work, in a single line of code you’ll be able to accomplish tasks that would normally take a significant amount of programming.

3.1. Arithmetic operators

3.2. Assignment operators

3.3. Comparison operators

3.4. Pattern matching and text manipulation

3.5. Logical and bitwise operators

3.6. Where() and ForEach() methods

3.7. Summary

sitemap