Chapter 12: Filtering and comparisons
The chapter introduces filtering as a way to narrow broad shell output to the items you want.
After this chapter you will be able to
- Purpose of narrowing results. Explain why you might narrow shell output instead of using every result, using processes or files that match a pattern as an example.
- Two filtering models. Distinguish between asking the retrieving cmdlet for only specified results and accepting all its results before using a second cmdlet to remove unwanted ones.
- Filter-left preference and limits. Explain why the section prefers filter left when the retrieving cmdlet supports it, and describe how a cmdlet’s available parameters can limit that choice.
- Filter-left principle. Explain that filtering left means placing filtering criteria as near the beginning of a command line as possible, so later cmdlets handle fewer unwanted objects and less unnecessary information may cross the network.
- Native filtering and Where-Object. Distinguish a cmdlet’s own filtering options from using Where-Object after retrieval: cmdlets vary in what they can filter, while Where-Object can filter any kind of object placed in the pipeline.
- Basic comparisons and case. Explain how PowerShell uses comparison operators to test equality or order and how case affects string comparisons.
- Combined and reversed conditions. Describe how -and, -or, and -not combine or reverse Boolean conditions, including a test of a Boolean property such as Responding.
- Text pattern comparisons. Distinguish wildcard comparisons with -like from regular-expression comparisons with -match, and describe their negative and case-sensitive forms.
- Where-Object filtering behavior. Trace how Where-Object applies a comparison to each incoming object and decides whether to pass it along.
- Property comparisons and discovery. Explain how $_ and a property name are used in a Where-Object comparison, and how to verify the property name.
- Iterative command-line model. Explain how to build a command by running a small version, examining its output, recalling it, and adding or changing one part at a time.
- Process memory pipeline stages. Trace the section’s example from getting processes through excluding PowerShell, sorting by virtual memory, keeping ten processes, and totaling their virtual memory.
- Early and source-side filtering. Explain why filtering should be placed in the first cmdlet when possible, or otherwise as early in the pipeline as possible. Describe why filtering remote data on the remote computer can be preferable to bringing all its objects back first.
- Nested pipeline execution and filtering. Trace the section’s parenthetical pipeline and explain why $_ refers to different objects in its two Where-Object filters.
- Selective filtering practice. Describe how to practice filtering the output of familiar cmdlets, using an example such as showing only cmdlets from Get-Command or only nonresponding computers from Test-Connection. State that Where-Object is useful when appropriate, not required for every filter.
What it builds on
- Connecting one command to another: Less work for you (chapter 6) · Commands can be connected so objects pass to later commands.
- Discovering objects: Get-Member (chapter 8) · Using Get-Member to discover object properties.
- Using object attributes, or properties (chapter 8) · Accessing an object's properties.
- Sorting objects (chapter 8) · Sorting objects.
- Using help to find commands (chapter 3) · Using help to find commands.
- Parenthetical commands (chapter 10) · Parenthetical commands supplying a result to another command
Section by section
Introduction
The chapter introduces filtering as a way to narrow broad shell output to the items you want.
- Purpose of narrowing results: Explain why you might narrow shell output instead of using every result, using processes or files that match a pattern as an example.
Why it matters: This gives you a reason to filter before learning how to do it.
Key terms: output, narrow down the results, match a pattern
12.1 Making the shell give you just what you need
This section introduces two ways to narrow PowerShell results and explains why filtering at the retrieving cmdlet is preferred when possible.
- Two filtering models: Distinguish between asking the retrieving cmdlet for only specified results and accepting all its results before using a second cmdlet to remove unwanted ones.
Why it matters: The distinction helps you choose where to narrow the results of a command. - Filter-left preference and limits: Explain why the section prefers filter left when the retrieving cmdlet supports it, and describe how a cmdlet’s available parameters can limit that choice.
Why it matters: Choosing an available filter on the first cmdlet avoids retrieving objects you intend to discard.
Key terms: filtering, filter left, early filtering, Get-Process, Get-ChildItem, -Path, Where-Object
12.2 Filtering left
This section explains why to filter early in a PowerShell command and when to use Where-Object instead of a cmdlet’s own filtering options.
- Filter-left principle: Explain that filtering left means placing filtering criteria as near the beginning of a command line as possible, so later cmdlets handle fewer unwanted objects and less unnecessary information may cross the network.
Why it matters: Filtering early can spare later commands—and sometimes your network—unnecessary work. - Native filtering and Where-Object: Distinguish a cmdlet’s own filtering options from using Where-Object after retrieval: cmdlets vary in what they can filter, while Where-Object can filter any kind of object placed in the pipeline.
Why it matters: When a command cannot filter everything you need, Where-Object gives you another way to narrow its results.
Key terms: filter left, filtering criteria, cmdlets, Get-Process, Name, Id, Where-Object, where, pipeline
12.3 Using comparison operators
This section explains how PowerShell comparison and logical operators test relationships between values and return True or False.
- Basic comparisons and case: Explain how PowerShell uses comparison operators to test equality or order and how case affects string comparisons.
Why it matters: These tests let you express which values should pass a condition. - Combined and reversed conditions: Describe how -and, -or, and -not combine or reverse Boolean conditions, including a test of a Boolean property such as Responding.
Why it matters: Combining and reversing conditions lets you state precisely which results you want. - Text pattern comparisons: Distinguish wildcard comparisons with -like from regular-expression comparisons with -match, and describe their negative and case-sensitive forms.
Why it matters: Choosing the right pattern operator lets you test text for the kind of match you mean.
Key terms: comparison operator, Boolean, -eq, -ne, -ge, -le, -and, -or, -not, subexpression
12.4 Filtering objects out of the pipeline
This section explains how Where-Object filters pipeline objects with a comparison, how to find the right property names, and when its simplified syntax can be used.
- Where-Object filtering behavior: Trace how Where-Object applies a comparison to each incoming object and decides whether to pass it along.
Why it matters: This is how you keep only the objects that meet your condition. - Property comparisons and discovery: Explain how $_ and a property name are used in a Where-Object comparison, and how to verify the property name.
Why it matters: A filter needs the object's real property name, not necessarily the heading shown on screen. - Simplified filter syntax limits: Distinguish when Where-Object's simplified syntax works from when the original syntax is still needed.
Why it matters: Recognizing both forms helps you read examples without assuming the shorter form works for every filter.
Key terms: Where-Object, pipeline, True, False, $_, WorkingSet, Get-Member, property, PM(MB), PM
12.5 Using the iterative command-line model
This section teaches readers to build a PowerShell pipeline one step at a time, checking each result before extending the command.
- Iterative command-line model: Explain how to build a command by running a small version, examining its output, recalling it, and adding or changing one part at a time.
Why it matters: Checking each stage makes a complex command easier to build and correct. - Process memory pipeline stages: Trace the section’s example from getting processes through excluding PowerShell, sorting by virtual memory, keeping ten processes, and totaling their virtual memory.
Why it matters: The order of the stages determines which processes contribute to the total. - Command discovery with help: Describe how to look for unfamiliar cmdlets or parameters needed while assembling the example.
Why it matters: You can extend a pipeline without already knowing every cmdlet or parameter.
Key terms: iterative command-line model, pipeline, filter left, Where-Object, Select-Object, virtual memory, -last, Help, Get-Command, wildcard search
12.6 Common points of confusion
Introduces a review of two common points of confusion about Where-Object.
12.6.1 Filter left, please
This subsection explains why filtering should happen as early in a pipeline and as close to the data source as possible.
- Early and source-side filtering: Explain why filtering should be placed in the first cmdlet when possible, or otherwise as early in the pipeline as possible. Describe why filtering remote data on the remote computer can be preferable to bringing all its objects back first.
Why it matters: Putting a filter earlier avoids making later steps handle objects you do not need.
Key terms: filter left, Where-Object, source of the data, PowerShell remoting
12.6.2 When $_ is allowed
Explains what $_ refers to in a pipeline and how its meaning changes when a parenthetical pipeline runs inside a larger command.
- Scope of the $_ placeholder: Explain when $_ is valid and what it represents as objects move through a pipeline.
Why it matters: Checking which objects reach a cmdlet tells you what its $_ refers to. - Nested pipeline execution and filtering: Trace the section’s parenthetical pipeline and explain why $_ refers to different objects in its two Where-Object filters.
Why it matters: Following execution order prevents you from mistaking strings in the inner filter for processes in the outer filter.
Key terms: $_ placeholder, pipeline, parenthetical command, nested pipelines, Get-Content, Where-Object, Get-Process, WorkingSet
12.7 Lab
12.8 Lab answers
12.9 Further exploration
This section suggests filtering exercises using familiar commands and cautions against using Where-Object in every case.
- Selective filtering practice: Describe how to practice filtering the output of familiar cmdlets, using an example such as showing only cmdlets from Get-Command or only nonresponding computers from Test-Connection. State that Where-Object is useful when appropriate, not required for every filter.
Why it matters: Trying filters on familiar output builds practice without making Where-Object a default choice.
Key terms: filtering, Where-Object, Get-Command, Test-Connection
Where this is used later
- The CIM way: Invoking methods (chapter 15) · Filtering results to the intended targets
- -contains and -in (chapter 26) · Using comparison operators in PowerShell.
- Using regex with -Match (chapter 21) · The later section develops the use of -match with regular expressions.
- Local vs. remote processing (chapter 13) · The distinction between local and remote processing builds on the idea of filtering near the data source.
- Making the cmdlets work for you (chapter 15) · Assumes prior exposure to the $_ placeholder.
Concepts explained in this chapter
- Where-ObjectWhere-Object is a PowerShell cmdlet that filters objects in the pipeline according to specified criteria.
- comparisonA comparison is an expression that evaluates a relationship between values and produces a Boolean result.
- filter leftFilter left is a result-narrowing technique in which the retrieving cmdlet is instructed to return only matching objects.
- filteringFiltering is the technique of selecting values or objects that satisfy specified criteria and excluding the rest.
- Select-ObjectSelect-Object is a PowerShell cmdlet that selects specified properties or creates customized objects from input objects.
- regular expressionA regular expression is a pattern notation for matching and processing text according to specified character sequences.
- Get-CommandGet-Command is a PowerShell cmdlet that retrieves commands matching specified names, verbs, nouns, or command types.
- moduleA module is a packaged unit of reusable code and related resources that can be loaded into a software environment.
- subexpressionA subexpression is an expression nested within a larger expression and evaluated as one operand or component.
- command-line interfaceA command-line shell is an interactive text-based interface that interprets and runs commands.
- -Syntax-Syntax is a Get-Command parameter that displays the syntax of a command.
- Get-MemberGet-Member is a PowerShell cmdlet that displays the members, including properties and methods, of input objects.
- column headerA column header is the displayed label identifying a column in tabular output, which may differ from the underlying property name.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
