liveBook · Manning

Filter left

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

Filter left is a result-narrowing technique in which the retrieving cmdlet is instructed to return only matching objects.

Filter left narrows results as close to the retrieving cmdlet as possible. The cmdlet returns only matching objects, reducing unnecessary work for later pipeline commands and possibly limiting information transmitted across a network. section 12.1 section 12.2

Select process names immediately

Get-Process -Name p*,*s* asks Get-Process to retrieve only process names matching the supplied patterns. This is preferable to retrieving every process and filtering afterward when the cmdlet supports the needed criteria. section 12.1

Use filter left with these practical limits in mind:

  • Check the retrieving cmdlet's parameters because each cmdlet implements filtering differently; Get-Process, for example, filters by Name or Id. section 12.2
  • Use Where-Object when the first cmdlet cannot express the required condition; it filters retrieved objects in the pipeline using comparison operators. section 12.2
  • Keep Where-Object as early in the pipeline as possible when it is necessary, even though filtering in the retrieving cmdlet is better. section 12.5
Get-Process -Name p*,*s*

Explore a direct Get-Process filter using wildcard name patterns. section 12.1

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Defines early filtering, explains its efficiency, and contrasts cmdlet parameters with later Where-Object filtering.
Examples
Uses Get-Process name patterns and contrasts them with process filtering through Where-Object.
What you can do after
Apply it via PowerShell cmdlet parameters or place Where-Object early when direct filtering is insufficient.
How it connects
Connects retrieving cmdlets with pipelines, Where-Object, comparison operators, and wildcard matching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.