Scripting Games 2012: Know When To Use Format-Table


This was a common occurrence during the Scripting Games. Someone writing a great script or one liner that gave me everything that I needed. But then right at the end I see something like this:

| Format-Table

Ok, what is the big deal you ask? They are only outputting the data as a nicely formatted table which makes it easier to read on the console. Well, I won’t deny that it looks pretty on the console, but guess what you just did?

Well, the problem is that once you use Format-Table (or any of the Format* cmdlets for that matter) you immediately lose the original object that was being outputted by the previous command. This means that you cannot pipe the output into most commands as its new object type is not usable by other commands (except for some commands that redirect the output such as Out-File and such). Sorting no longer becomes an option as it doesn’t know how to deal with the new object. Don’t even bother trying to run an Export-CSV to output the data as a CSV file, because trust me, it will give you a pretty nicely unusable file that looks nothing like what you saw in the console.

Lets look as some simple examples here. First lets do a WMI query and look at an object and then pipe it into a Format-Table and see the object.

Get-WmiObject Win32_Service


Simple output from Get-WMIObject showing some information as a list. Not the easiest to read but it works great.

Get-WmiObject Win32_Service | Get-Member


As you can see, we have access to all of the properties and methods with this object. Now lets blow this useful object away with Format-Table.

Get-WmiObject Win32_Service | Format-Table


Sure, the output does look nice, but lets look under the hood at the new object.

Get-WmiObject Win32_Service | Format-Table | Get-Member


What you are seeing a whole lot of formatting of the object. All useful methods are gone and you cannot even see the properties anymore. Which leads into the next example: Sorting.

Get-WmiObject Win32_Service | Format-Table | Sort Name

What do you think will happen?


If your answer was fail, then congrats! As you can see this fails because of the new formatted object. If you try to pipe this into another cmdlet other than one that performs a redirection of output, you will become very familiar with this error message. What about using ForEach? Well, no error but at the same time, no output either.

Get-WmiObject Win32_Service | Format-Table | ForEach {$}


Now I mentioned how writing to a CSV with Export-CSV would end in failure so let me show you just how pretty this output will be.

Get-WmiObject Win32_Service | Format-Table | Export-CSV -NoTypeInformation FTReport.csv
Invoke-Item FTReport.csv


Now that is a report that I would not want to send to my boss or anyone else for that matter. All you get now is a bunch of useless garbage that is taking up hard drive space.

Now I am not saying that you,  should never use Format-Table (or the other Format* cmdlets), I am just saying that use caution with them. In fact, unless you have a specific requirement to use these commands in  a script or function, DO NOT use them at all in your code! Leave it to the people using the script/function to decide if how they want to handle the object that you give them. Remember, the best thing you can do is give the original object if possible. Then they can do whatever they wish with it.

It is worth noting that some cmdlets output a table by default (Get-Process and Get-Service for instance) which can still be piped into another command or you can export to a CSV using Export-CSV. This is due to the  formatting files that are loaded when PowerShell starts up that define what is shown in a table format or list format but does not actually affect the original object itself. See this link some more information about these files. You can also run this command to get more information about formatting:

Help about_format

While not exactly on the same subject, one thing to keep in mind with one liners is this: Filter-Select-Sort. You want to filter as early as you can in the code to keep the returned objects as few as possible, then select only what you need and then sort/format the output.

So in closing, remember that while Format-Table does provide some nice output on the console, it can and will wreak havoc if you attempt to do much more with the newly formatted objects. Watch out when you use it!

This entry was posted in powershell, Scripting Games 2012 and tagged , , , . Bookmark the permalink.

3 Responses to Scripting Games 2012: Know When To Use Format-Table

  1. Pingback: Scripting Games 2013: Event 2 ‘Favorite’ and ‘Not So Favorite’ | Learn Powershell | Achieve More

  2. Pingback: Search Event Log – 2012 PowerShell Scripting Games Beginner Event #9 | Mike F Robbins

  3. Chris Manning says:

    This was the mistake that got me out of the top five. Well there were other mistakes this was just the final one. Thanks for all your advice and comments during the 2012 scripting games.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s