PowerShell Genie

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.

Interactive guide 9 min practice Windows PowerShell 5.1 and PowerShell 7

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
  1. Stage 1Get-ServicePositional values or no named parameters
  2. Stage 2Where-Object-eq
  3. Stage 3Where-Object-eq
  4. Stage 4Sort-ObjectPositional values or no named parameters
  5. Stage 5Select-ObjectPositional values or no named parameters

    Build the reasoning

    Collect the objects

    1PS> Get-Service

    The source returns all six fictional services. Name, Status and StartType are available to the next stage.

    Keep and order the rows

    1PS> Where-Object Status -eq 'Stopped' | Sort-Object Name

    Fax, Spooler and WinRM survive. Sorting after the filter gives a predictable report order.

    Choose the report fields

    1PS> Select-Object Name,Status

    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

    Fictional input objects · 6 rows
    NameStatusStartType
    BITSRunningManual
    SpoolerStoppedAutomatic
    WinRMStoppedAutomatic
    W32TimeRunningManual
    EventLogRunningAutomatic
    FaxStoppedDisabled

    Select your predicted objects before revealing the output. Then edit one stage and inspect the change.

    Predict which objects survive the worked pipeline

    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

      1PS> Get-Service | Select-Object Name | Where-Object Status -eq 'Stopped'

      The Name-only objects no longer carry Status. Filtering an absent property cannot produce this report.

      Formatting before exporting

      1PS> Get-Service | Format-Table Name,Status | Export-Csv ./services.csv

      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
      1PS> Get-Service | Where-Object Status -eq 'Stopped' | Where-Object StartType -eq 'Automatic' | Sort-Object Name | Select-Object Name,Status

      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

      Final challenge not completedChoose another guide

      Practise this

      Choose two service properties

      Return only Name and Status for all services.

      Review the lesson

      Genie guideBuild 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.