PowerShell Genie

Select the two busiest small processes in PowerShell

Exercise 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Write a pipeline, run it against the sample data, and check your result.

An example to inspect

Preview the example. Sign in for the full lesson and activities.

Get-Process | Where-Object WS -lt 100MB | Sort-Object CPU -Descending | Select-Object -First 2

Target

Among processes using less than 100 MiB of working-set memory, return the two with the highest cumulative CPU seconds, highest first.

Expected result

pwsh, spoolsv

Try the task

Show a worked solution
Reveal one approach
1PS> Get-Process | Where-Object WS -lt 100MB | Sort-Object CPU -Descending | Select-Object -First 2

Try it in the lab

Write your pipeline below and run it against the fictional sample data, then check your result against the expected answer.

Interactive simulation · fictional data

PowerShell skills

Process objects · Filtering · Sorting · Selection

Build the answer in three decisions

Read the pipeline from left to right. Each stage receives objects from the stage before it.

Collect

Get-Process

Start with 5 fictional process objects. Keep them as objects so later stages can inspect their properties.

Shape

Where-Object WS -lt 100MB | Sort-Object CPU -Descending

Apply the condition or ordering required by the task. Filter memory, rank the surviving processes by CPU, and then take two. CPU is accumulated time, not a percentage.

Verify

Check order and returned names

The fixture should return pwsh, spoolsv. Order matters for this exercise.

Reason through the pipeline

The source stage returns 5 process objects. Then the pipeline applies Where-Object WS -lt 100MB, Sort-Object CPU -Descending, Select-Object -First 2 without converting the objects to display text. That keeps the Name property available for the checker and for any later pipeline stage.

Why common attempts fail

Do not hard-code the expected names; express the filter against an object property so the pipeline still makes sense when the fixture changes. Do not select the first rows before sorting when the task asks for a highest, lowest, first, or reverse-ordered result. Use Select-Object after the important filtering or ordering work; selecting too early can discard properties needed by later stages.

What the checker accepts

The evaluator runs the supported subset against the same fictional fixture. It expects pwsh, spoolsv. The returned order is part of the answer. Equivalent supported syntax is accepted.

Verify another attempt

First run Get-Process by itself and inspect the fixture. Then add one stage at a time and compare how many objects remain. This isolates the stage that changes the result and makes the final pipeline easier to explain and debug.

Primary documentation

PowerShell pipeline behavior

Next exercise
Genie guideSelect the two busiest small processes in PowerShell

Write a pipeline, run it against the sample data, and check your result.