liveBook · Manning

Where-Object

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

Where-Object is a PowerShell cmdlet that filters objects in the pipeline according to specified criteria.

Where-Object is PowerShell’s generic pipeline filter. It examines objects one at a time and passes along only those that satisfy a comparison; its alias is where. section 12.2 section 12.4

Place filtering as far left in a pipeline as practical. Removing unwanted objects early reduces later work and may reduce unnecessary network data, although individual cmdlets sometimes offer more limited filtering of their own. section 12.2 section 12.5

Keep processes using more than 100 MB

Get-Process | Where-Object -FilterScript {$_.WorkingSet -gt 100MB} The -FilterScript parameter is positional, so this can also be written as Get-Process | Where-Object {$_.WorkingSet -gt 100MB}. For each process, $_ represents the current object, .WorkingSet selects its property, and -gt performs the comparison. section 12.4

Get-Process | Where-Object -FilterScript {$_.WorkingSet -gt 100MB} 

Use this runnable listing to see the full filter-script form for selecting processes by WorkingSet. section 12.4

The simplified syntax supports a single comparison: Get-Process | where WorkingSet -gt 100MB. Use the script-block form when combining tests, such as WorkingSet -gt 100MB -and CPU -gt 100. section 12.4

Verify property names with Get-Member rather than trusting displayed column headings. A formatted heading such as PM(MB) may represent a property with a different name. section 12.4

Build a process query iteratively

Get-Process | Where-Object { $_.Name -notlike 'pwsh*' } | Sort-Object VM -Descending | Select -First 10 | Measure-Object -Property VM -Sum Run a command, inspect its results, recall it with the up arrow, and add the next pipeline step. This makes filtering, sorting, selection, and measurement easy to test incrementally. section 12.5

$_ is valid where PowerShell expects the current pipeline object. In nested pipelines, each Where-Object receives its own input: strings can be filtered first, followed later by process objects and their properties. section 12.6.2

Where-Object also works with files, modules, dates, event records, and text-matching conditions, including filters combined with -and and -match. section 12.8 section 21.4

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

How deep it goes
Explains per-object evaluation, comparison syntax, $_, nested pipelines, property names, and iterative construction without descending into implementation internals.
Examples
Demonstrates process, file, module, date, event-log, regex, and nested-content filters.
What it assumes
Assumes basic pipelines and objects while teaching comparison operators, property discovery, $_, and filter placement.
What you can do after
Apply it via PowerShell pipelines to retain objects matching property comparisons, wildcard tests, compound conditions, or regular expressions.
How it connects
Connects pipeline filtering with comparison operators, Get-Member, Sort-Object, Select-Object, Measure-Object, and Select-String.

Also explained in

Compare how 2 books explain Where-Object →

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.