Chapter 9. Filtering and comparisons

 

So far, we’ve been working with whatever output the shell gave us: all the processes, all the services, all of the event log entries, all of the hotfixes. That won’t always be what you want, though. In many cases, you’ll want to narrow the results down to a few items that specifically interest you. That’s what you’ll learn to do in this chapter.

9.1. Making the shell give you just what you need

The shell offers two broad models for narrowing down 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 only retrieve what you’re specifically after. In the second model, you take everything that the cmdlet gives you and then use a second cmdlet to filter out the things you don’t want.

Ideally, you’ll use the first model, which I call early filtering, as much as possible. It may be as simple as telling the cmdlet what you’re after. For example, with Get-Service, you can tell it which service names you want:

Get-Service -name e*,*s*

But if you want Get-Service to only return running services, 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.

9.2. Filter left

9.3. Comparison operators

9.4. Filtering objects out of the pipeline

9.5. The iterative command-line model

9.6. Common points of confusion

9.7. Lab

9.8. Ideas for on your own