12 Filtering and comparisons

Up to this point, you’ve been working with whatever output the shell gave you: all the processes, filesystem objects, and various Azure commands. But this type of output isn’t always going to be what you want. Often you’ll want to narrow down the results to a few items that specifically interest you, such as getting processes or files that match a pattern. That’s what you’ll learn to do in this chapter.

12.1 Making the shell give you just what you need

The shell offers two broad models for narrowing results, and they’re both referred to as filtering. In the first model, you try to instruct the cmdlet that’s retrieving information for you to retrieve only what you’ve specified. In the second model (discussed in section 12.5), which takes an iterative approach, you take everything the cmdlet gives you and use a second cmdlet to filter out the things you don’t want.

Ideally, you’ll use the first model, which we call filter left, as much as possible. It may be as simple as telling the cmdlet what you’re after. For example, with Get-Process, you can tell it which process names you want:

Get-Process -Name p*,*s*

But if you want Get-Process to return only the processes with more than 1 GB of memory, regardless of their names, you can’t tell the cmdlet to do that for you, because it doesn’t offer any parameters to specify that information.

12.2 Filtering left

12.3 Using comparison operators

12.4 Filtering objects out of the pipeline

12.5 Using the iterative command-line model

12.6 Common points of confusion

12.6.1 Filter left, please

12.6.2 When $_ is allowed

12.7 Lab

12.8 Lab answers

12.9 Further exploration