Q. Why can’t I pipe Format-Table to Export-CSV?

Don Jones

April 29, 2010

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. You can pipe Format-Table to Export-CSV, but it won’t be very pretty. All of the Format cmdlets produce a special kind of formatting objects that tell PowerShell how to construct a visual display. These objects aren’t really useful to any cmdlet except an Out cmdlet. That’s why I like to follow the rule “Format Right,” meaning the Format cmdlet should be the last, or right-most, cmdlet on any command line. It can be the second-last cmdlet only if it’s followed by an Out cmdlet.

The good news is that there’s really no reason to pipe Format-Table to Export-CSV. Once something is in a table format, it can’t become a comma-separated value (CSV) file. However, before you put it into a table format, you can use Export-CSV just fine:

Get-Service | Export-CSV

The reason a lot of people want to throw Format-Table in there is to limit the number of properties being exported:

Get-Service | Format-Table Name,DisplayName,Status | Export-CSV

That won’t work, but you can accomplish the task by using Select-Object instead:

Get-Service | Select-Object Name,DisplayName,Status | Export-CSV

Select-Object is able to pick and choose specific properties without changing the objects into those special formatting objects in the way that the Format cmdlet does.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like