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.
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.
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.
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
Note 4: #Type System.Diagnostics.Process in the first row
is the .NET Framework type of the object.
Guy Recommends: SolarWinds' Free 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.
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.
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.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
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.
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.
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
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.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.