header image

Scripting Games 2011 Commentary: I–Exporting after using a format cmdlet

Posted by: | April 24, 2011 | No Comment |

As the final judging of the Scripting Games takes place today I thought it time to blog about a few things I noticed while judging.  These comments are not about solving the events but about using PowerShell.  These comments are in no particular order – just things I observed.

This is a standard pipeline. I would suggest that you try these snippets to see what is happening.

Get-Process | select -First 3 | Format-Table

There are a number of ways to get this information in to a file

Get-Process | select -First 3 | Export-Csv test1.csv –NoTypeInformation

This stores the whole objects worth of data. Using –NoTypeInformation means that it is returned as a PSCustomObject when we import it.

Alternatively we can do this

Get-Process | select -First 3 | Out-File test1.txt

This stores just what we would have seen on screen – ie the results of the default display

PS> get-content test1.txt

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
——-  ——    —–      —– —–   ——     — ———–
     57       3     1764       5196    58     1.64    808 conhost
     30       2      504       2124    20            1404 conhost
     57       3     1760       5156    54     0.20   1688 conhost

When we read the file contents we get a collection of strings.  All object information is lost.


Get-Process | select -First 3 | Format-Table | Out-File test2.txt

is equivalent to our out-file example above in that we get strings


Get-Process | select -First 3 | Format-Table | export-csv test2.csv

Outputs the formatting objects NOT the data.  Oops.


When exporting data to files make sure you are getting what you expect. Don’t export after a format statement. Use Export-Csv if you want objects and Out-File if you just want the data points.

under: PowerShellV2