4 min readRishi

PowerShell: Format-Table Breaks the Next Command

PowerShell: Format-Table Breaks the Next Command

Get-Service | Format-Table | Export-Csv services.csv produces a CSV full of garbage, or an error about formatting records, depending on the version and the next command. Format-Table did what it is for. It turned service objects into instructions for the console. Export-Csv then serialized those instructions. The file is not a list of services.

Format cmdlets are the end of the object pipeline. They belong in the last position, when a person is looking at the screen. They do not belong in the middle of a script.

What to use instead

You wantedWrite this
A table on screen, and then stopGet-Service | Format-Table -AutoSize as the last line
A CSV of real propertiesGet-Service | Select-Object Name, Status, StartType | Export-Csv services.csv -NoTypeInformation
Only some columns, still objectsSelect-Object. Never Format-Table
A string for a log line"$($_.Name) is $($_.Status)" or -join. Not Out-String on the whole table unless you mean to log the rendering
JSON for another systemSelect-Object ... | ConvertTo-Json. Format cmdlets have already lost the properties

Select-Object chooses properties and emits objects. Format-Table chooses columns and emits format data (Microsoft.PowerShell.Commands.Internal.Format.*). The names sound similar. The next command can tell them apart, and only one of them is still data.

If you want to see what you are about to export, run the Select-Object pipeline once without Export-Csv. The host will format those objects for you. You do not need Format-Table to preview.

The bug in the middle of a function

function Get-RunningServices {
    Get-Service |
        Where-Object Status -eq Running |
        Format-Table Name, Status
}

Callers who do $svc = Get-RunningServices; $svc.Name get nothing useful. The function displayed a table and returned formatting objects. Return the objects. Let the caller format.

function Get-RunningServices {
    Get-Service | Where-Object Status -eq Running | Select-Object Name, Status, StartType
}

If the function is interactive and you want a table when someone runs it at the prompt, that already happens. PowerShell formats objects for the host when they reach the end of the pipeline. You do not have to call Format-Table to get a table. You call it when you need widths, a custom column, or to force the view. Do that in the script the human runs, not in the function other scripts call.

A module that returns format data is a breaking change that will not show up until someone pipes it. Pester should assert .Name and .Status on the result, not a screenshot of a table.

Out-String, Format-List, Format-Wide, Out-GridView

Out-String collapses the stream into one string. A later Where-Object filters lines of text, badly, because a wrapped line is not a row. Use it when you are building an email body or a log blob and you want the rendered table. Do not use it as a way to "stringify an object" for JSON.

Format-List is the vertical version of Format-Table and is just as final. Format-Wide is the same family.

Out-GridView is a UI, not a format cmdlet, but it is also a sink: it does not pass objects through. -PassThru is the exception — it returns what the user selected, as the original objects. Without -PassThru the pipeline ends.

ConvertTo-Csv / Export-Csv need objects with properties. After a format cmdlet they will write columns like ClassId2e4f51ef21dd47e99d3c952918aff9cd — the format record's internal fields. If you see that header, you formatted too early.

Finding it in a repo

Search scripts for Format-Table, Format-List, Format-Wide, and Out-String. If the next pipe is anything other than Out-Host, Out-File of a report you intend to be text, or the end of the script, delete the format cmdlet and project with Select-Object.

This is the same class of mistake as a digitally unsigned script that only fails on the machine with a stricter execution policy: the interactive session hides it. The pipeline does not.

The CSV, the JSON, and the next function will start receiving the objects you thought you were sending all along.

Keep reading

Newsletter

New posts, straight to your inbox

One email when a new post goes up. Double opt-in, unsubscribe anytime. Not a daily digest.

Comments

  • No comments yet. Be the first.