PowerShell Genie

The pipeline

Build a service report by filtering objects, selecting properties and checking what each stage receives.

Lesson 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Overview

Suppose you need the names of running services for a handover. Start with Get-Service, whose output contains service objects. The columns PowerShell chooses to display are only a view of those objects. You can use a property that is not shown in the default table.

In the example, Where-Object checks Status and passes matching objects onward. Select-Object then creates a smaller view containing Name and Status. If you select only Name before filtering on Status, the property needed by the filter is no longer present.

Many pipeline stages stream their input, but some need the collection first. Sort-Object must compare values before it can produce a fully sorted result. Do not assume that every pipeline uses constant memory or returns immediately.

Keep data operations ahead of formatting. Format-Table emits formatting instructions for display; an export after it receives those instructions instead of the service records. Native programs may also send text through the pipeline, so check the source type instead of assuming every input is a rich object.

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

    Watch objects survive each stage

    Change the status, predict the count, then step through the pipeline. Click any service object to inspect its properties.

    Get-Service emits service objects with all their properties.

    Where-Object evaluates each object and only forwards matching objects.

    Select-Object Name shapes the result at the end. The filter happened while the objects still had their full property set.

    The object workshop

    Watch your command work.

    Change the command. Step through the pipeline. Inspect what survives.

    5 fictional services

    Browser simulation · fictional services · Ctrl+Enter to run

      Object properties

      Enable JavaScript to step through this example. The lessons and command reference remain available.

      Your task: return the stopped service, with exactly Name and Status.

      What happens

      Run the existing example, then change Running to Stopped. Inspect which names disappear. Next, remove Status from the final selection: the filter still works because it ran before that property was removed.

      The first five results are the first five received, not necessarily the five you would choose for a report. Add Sort-Object Name before Select-Object -First 5 when alphabetical order is part of the requirement.

      1 · Get-Service emits objects

      Seven fictional service objects enter the pipeline — BITS, Spooler, WinRM, EventLog, Dnscache, W32Time, wuauserv. Each carries Name, Status and DisplayName.

      Get-Service

      2 · Where-Object keeps matches

      Status -eq 'Stopped' is tested against each object. Only WinRM passes — the other six are dropped at this stage.

      | Where-Object Status -eq 'Stopped'

      3 · Sort-Object orders survivors

      The surviving objects are arranged by Name. With one result the order is trivial — but the stage still runs, and with more matches it decides what you see first.

      | Sort-Object Name

      Worth remembering

      Filtering changes which records remain; selection changes which properties remain. Sort before taking the first records when order matters.

      Primary documentation

      Microsoft Learn: pipelines

      Try it

      Browser simulation · fictional data; no changes to your computer.

      Your task: Return the first three running service names alphabetically. Keep Name in the result.

      Interactive simulation · fictional data
      Hint

      Filter on Status, sort on Name, then take three.

      Compare a solution
      Get-Service | Where-Object Status -eq 'Running' | Sort-Object Name | Select-Object -First 3 Name

      Filters services down to running ones, then keeps three and two columns.

      Show reference output

      Sample output · fictional data, shaped like the real thing

      Name      Status 
      --------  -------
      BITS      Running
      Dnscache  Running
      EventLog  Running

      FIRST REPORT · KEEP MATCHING OBJECTS

      Practise without copying

      Return only the stopped service, with exactly Name and Status. Filter while Status is still available.

      How the worked example fits together

      Get-Service produces seven objects in this fixture. Where-Object compares Status on each object. Only WinRM passes the stopped filter. Select-Object then keeps the two report columns; filtering after selecting only Name would lose Status.

      Get-Service | Where-Object Status -eq 'Stopped' | Select-Object Name, Status

      Where-Object keeps matching objects; Select-Object shapes the surviving record. A stopped manual service is not automatically a fault.

      Bounded browser simulation · fictional services · no computer or tenant access.

      Predict the result, write your pipeline, then run it.

      Practise this

      Filter stopped services

      Return the complete records for stopped services.

      Review the lesson

      Keep going

      Next: Get-Member
      Genie guideThe pipeline

      Build a service report by filtering objects, selecting properties and checking what each stage receives.