Filtering Cmdlets

PowerShell also has a few useful filtering cmdlets:

Where-Object (alias where and ?)—Participates in a pipeline by helping to narrow down the objects passed along the pipeline based on some specific criteria.

ForEach-Object (alias foreach and %)—Participates in a pipeline by applying a scriptblock to every object passed along the pipeline.

By looking at a few files and a directory contained within a test directly, we can easily demonstrate the use of both cmdlets:

image

In this example, first the ForEach-Object cmdlet is demonstrated, where each object passed along from the dir command is acted on, and the name of the object (filename ...

Get Microsoft® SQL Server 2008 R2 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.