Spreadsheet PowerShell Export-CSV

Spreadsheet PowerShell Export-CSV

Comma separated values are the ideal format for translating lines of text into rows in a spreadsheet.  Since much of PowerShell’s output is in columns, all we need is a cmdlet which will export CSV data, then we can exploit Excel’s mathematical capability.

PowerShell Export-CSV Topics

 ♣

PowerShell Pre-requisites and Checklist

In the case of Windows 7 and later, you don’t need to download any extra files, just: ‘Add Feature’ –> Windows PowerShell.  However, for older operating systems, there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  For such legacy systems only, you need to download PowerShell from Microsoft’s site.

Once you have installed PowerShell 2.0 or later, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save buying a text editor.

Example 1: PowerShell Export-CSV

Scenario: We wish to analyse the memory usage of our computer’s processes in a spreadsheet.  The knack is to export all the raw data collected by PowerShell then use the capability of Excel to manipulate and refine the figures.

# PowerShell Export-CSV Example Processes
Clear-Host
$File = "E:\PowerShell\Process\mem3.csv"
Get-Process | Export-CSV $File

Note 1:  The reason that I used the variable $File is that I want to remind you to change the value for the path before running this script on your machine.

Note 2:  Writing this note reminds me of reading those silly warning notices on packages, but I feel I must remind newbies that you really do need a spreadsheet like Excel to open the resulting csv file.

Note 3:  Save Frustration
You cannot use Format-Table with the Export-CSV cmdlet.  However you can use Select-Object to choose just the properties that you want to see in the spreadsheet.  See Example 2.

Excel Spreadsheet Screenshot

PowerShell Export-CSV

Note 4: #Type System.Diagnostics.Process in the first row is the .NET Framework type of the object.

Guy Recommends:  SolarWinds’ Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

Example 2: Export-CSV with Select-Object

Because you cannot use Format-Table, Select-Object will allow you to choose just the properties that you need for the export from PowerShell to spreadsheet.

# Export-CSV PowerShell Spreadsheet
Clear-Host
$FilePath = "E:\PowerShell\Process\Serv5s.csv"
Get-Service | Select-Object DisplayName, CanStop | Export-CSV $FilePath

Note 5: While I chose DisplayName and CanStop, you could research other properties in the underlying cmdlet Get-Service.

Example 3: Filtering With Where-Object Clause

Scenario: You need to export a list of just services that are running, you don’t need those that are stopped.  Export-CSV, with a where {clause}, is a useful technique for filtering the data before you export to the spreadsheet.

# PowerShell Export-CSV Example Services Running
Clear-Host
$FilePath = "E:\PowerShell\Process\Serv1s.csv"
Get-Service | Where{$_.status -eq "Running"} |Export-CSV $FilePath

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Research Export-CSV Parameters

While you cannot append format-table, you can research the properties of Export-CSV

# PowerShell Export-CSV Parameters
Clear-Host
Get-Help Export-CSV -full

Checking the help file will reveal useful parameters, for instance you can change the delimiter from the default comma to the semicolon.  Another useful parameter, albeit one with a strange name, is -NoClobber, this prevents you from over-writing a previously created file.

Export-CSV Alias EpCsv

With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner.  One obvious example is that you can abbreviate Format-Table to ft.  As you increase your range of PowerShell commands, keep an eye out for another PowerShell Alias, for example gci (Get-Childitem).

Researching Similar PowerShell Cmdlets

# PowerShell CSV Cmdlet Research
Clear-Host
Get-Command -Noun CSV

As expected there is an Import-CSV cmdlet, but you may not realize there is a ConvertTo-Csv cmdlet (also Export-CliXml)  PowerShell spreadsheet research always throws up at least one surprise.

Review SolarWinds Free CSVDE Import Tool »

Summary of PowerShell Export-CSV

One thing I have missed in PowerShell is the flexibility of a spreadsheet.  While PowerShell has a serviceable ‘If’ statement, in truth I find Excel’s battery of functions much easier to deliver the results that I need.  Also controlling the decimal point is only ever one click away in a spreadsheet.  Now thanks to Export-CSV I have the best of both worlds, PowerShell at the backend collecting the raw data, Excel at the frontend displaying the final figures and charts.  Incidentally, a bonus of using this duo is that I tend to save results more than when I use PowerShell alone.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell output tutorials:

PShell Home   • Out-File   • Out-GridView   • ConvertTo-Csv   • ConvertTo-Html   • ConvertFrom-Csv

Tee-Object   • Import-CSV   • Format-Table   • PowerShell Here-String  • ConvertFrom-JSON

Export-CliXml   • Format-List   • Read-Host    • PowerShell Get-History   • -f format   • Pipe to file

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.