Skip to content

Lesson 2 of 13 · 13m

The Display Layer Lies

Default console views show a projection of each object, and Format-* cmdlets emit dead formatting records that destroy downstream pipelines.

Get-Service prints three columns: Status, Name, DisplayName. The ServiceController object behind that table has roughly forty members — CanStop, DependentServices, ServicesDependedOn, StartType, BinaryPathName on newer builds, UserName, Site, Container. What the console shows you is a view, not the object.

A view is a formatting decision

The table you see is produced by PowerShell's formatting subsystem, not by the object. In Windows PowerShell 5.1 the rules live in *.format.ps1xml files under $PSHOME — DotNetTypes.format.ps1xml declares the default table view for System.ServiceProcess.ServiceController and picks which three properties fit eighty columns. In PowerShell 7 most of that metadata is compiled in, same idea. The view is chosen by type name and tuned for a human at a console, not for a script.

This is why Get-Member exists. Get-Service spooler | Get-Member lists every property, method, alias property, and script property the object actually carries. Get-Member -Force adds the members the type system hides — psobject, pstypenames, psadapted, psextended — the wrapper PowerShell puts around every object to attach extended type data. Alias properties like RequiredServices (which points at ServicesDependedOn) exist because the Extended Type System added them; in 5.1 that data shipped in types.ps1xml.

The mental model to kill: "the output I saw is the data." No. The output was one projection of the data. Get-Process | Select-Object * and Get-Process | Format-List * are the interrogation tools. When a property you need isn't in the default view — StartType on a service, Path on a process — it is still on the object. Ask for it.

Format-* is a terminal

Format-Table, Format-List, Format-Wide, and Format-Custom do not transform objects. They consume them and emit formatting records — FormatStartData, GroupStartData, FormatEntryData, GroupEndData, FormatEndData — which Out-Default, Out-Host, and the console renderer know how to draw. Put one anywhere before the end of a pipeline and downstream cmdlets receive those wrapper objects, not your data.

The canonical faceplant:

Get-Process | Sort-Object CPU -Descending |
    Format-Table Name, CPU -AutoSize | Export-Csv top.csv

top.csv now contains rows whose properties are ClassId2e4f51ef21dd47e99d3c952918aff9cd, shapeInfo, and groupByEntry. The processes are gone. The fix is to understand the division of labor: Select-Object chooses data, Format-* chooses presentation, and presentation is the last thing that happens — usually implicitly, via Out-Default when the pipeline ends at the console.

Select-Object has its own split personality. Select-Object Name, CPU builds a new PSCustomObject carrying only those properties — a projection. Select-Object -First 10 picks rows. Select-Object -ExpandProperty Name abandons the object entirely and returns the property's values. All three look similar; they do unrelated things. Mixing them up is how you end up sorting strings you meant to sort objects.

Same story with Out-Host -Paging and Out-GridView — they're console presenters. Out-GridView -PassThru is the one scripting exception: it pops a selectable grid and passes the picked rows back into the pipeline as objects, genuinely useful in interactive tooling and useless in a scheduled task.

Calculated fields without leaving object-land

The projection form everyone uses daily is the hashtable field: @{n='Name'; e={expression}} (or Name/Expression spelled out). The expression runs with $_ set to the current object:

Get-CimInstance Win32_LogicalDisk -Filter 'DriveType=3' |
    Select-Object DeviceID,
        @{n='SizeGB';  e={[math]::Round($_.Size/1GB,1)}},
        @{n='FreePct'; e={[math]::Round(100*$_.FreeSpace/$_.Size,1)}},
        @{n='Label';   e={$_.VolumeName}} |
    Where-Object FreePct -lt 10 |
    Export-Csv -NoTypeInformation low-disk.csv

Everything stays typed until Export-Csv. You can sort FreePct numerically, feed it to Where-Object, mail it, graph it. The moment you'd done this with Format-Table and text scraping, none of that would be possible.

The practice

When a cmdlet's output doesn't show what you need, don't reach for text parsing — run | Get-Member, find the property, and project it with Select-Object. Reserve Format-Table/Format-List for the final step of interactive work, and keep them out of scripts entirely: in a script, the pipeline should end in data (Export-Csv, ConvertTo-Json, a return value) or in a deliberate Out-*. If you ever find yourself about to pipe a Format-* into anything, the design is already wrong — go back one stage and select the data instead.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.