Build a report from PowerShell objects
Six service objects arrive. Your report needs only the stopped ones, in a stable order, with two useful fields. Build that report without losing the properties the next stage needs.
Editable pipeline · fictional data · Microsoft documentation
The task
An object carries named properties even when the console shows only a few columns. Where-Object keeps or drops the whole object. Sort-Object changes its position. Select-Object shapes the properties of the output object. None of these read-only stages starts or stops a service.
- Read a pipeline as a sequence of object transformations.
- Filter before projecting away a property.
- Verify the rows, fields and order of a report.
Genie tip: Read a pipeline as a sequence of object transformations.
See the objects move
Inspect this authored example with fictional data in the bounded browser model. The numbered stages show how its objects change.
Get-Service | Where-Object Status -eq 'Stopped' | Where-Object StartType -eq 'Automatic' | Sort-Object Name | Select-Object Name,Status- Stage 1Get-Service
Positional values or no named parameters - Stage 2Where-Object
-eq - Stage 3Where-Object
-eq - Stage 4Sort-Object
Positional values or no named parameters - Stage 5Select-Object
Positional values or no named parameters
Build the reasoning
Collect the objects
The source returns all six fictional services. Name, Status and StartType are available to the next stage.
Keep and order the rows
Fax, Spooler and WinRM survive. Sorting after the filter gives a predictable report order.
Choose the report fields
The report ends with two properties. StartType is no longer present, so any filter that needs it belongs earlier.
Try it: predict, run and explain
| Name | Status | StartType |
|---|---|---|
| BITS | Running | Manual |
| Spooler | Stopped | Automatic |
| WinRM | Stopped | Automatic |
| W32Time | Running | Manual |
| EventLog | Running | Automatic |
| Fax | Stopped | Disabled |
Select your predicted objects before revealing the output. Then edit one stage and inspect the change.
Ctrl / Command + Enter runs the editor. This bounded model uses the displayed fixture; it does not run PowerShell on a computer.
Predict first, then run the pipeline.
Find the mistake
Selecting too early
The Name-only objects no longer carry Status. Filtering an absent property cannot produce this report.
Formatting before exporting
Format-Table creates formatting instructions. Use Select-Object to shape data; format only when the next destination is a human-readable display.
A colleague filters on StartType after Select-Object Name,Status. What is the smallest useful repair?
Prove it: write it alone
Return only stopped services whose startup type is Automatic. Sort by Name and return exactly Name and Status. Do not name individual services in your command.
The checker also runs your command against a fresh fixture. Matching today’s names alone is not enough.
Show a worked solution
Read the stages, close this explanation, then write and test your own attempt.
Take it to a real environment
In a real report, inspect Get-Service | Get-Member when a property is unfamiliar. Keep the working pipeline as objects until the final display or export. An empty result is evidence to investigate, not proof that your query was correct.
Verify the details
Practise this
Choose two service properties
Return only Name and Status for all services.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.