O'Reilly logo

Monad (AKA PowerShell) by Andy Oakley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Work with Structured File Formats

Text files are great for storing simple, human-readable information, but they're not ideally suited to representing the structured data that we're becoming accustomed to in MSH. More often than not, we're dealing with objects and properties rather than strings and characters. MSH supports an additional data format with ease: comma-separated variables (CSV).

With two additional cmdlets for working with comma-separated data, easy-to-use spreadsheet applications can be combined with the power of the shell. Whether used for input (managing a task list in a friendly interface) or output (such as drawing graphs and performing statistical analysis), the CSV format can be very useful.

How Do I Do That?

Like set-content, the export-csv cmdlet takes a filename into which it writes a sequence of lines—one per object—with each field separated by a comma:

    MSH D:\MshScripts> get-process | export-csv -NoTypeInformation processes.csv

The resulting file can now be used in an application such as Microsoft Excel or in other tools that can process CSV files. Figure 5-1 shows a graphical representation of handle count by process. The -NoTypeInformation option is used to suppress the first line of the output (which would otherwise contain MSH-specific formatting information).

Graph generated from get-process data

Figure 5-1. Graph generated from get-process data

export-csv also has a mirror cmdlet, import-csv, that ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required