Dealing with CSV Files

PowerShell provides two cmdlets that help greatly when you are dealing with files in the comma-separated value (CSV) format:

Import-CsvThis cmdlet reads in a CSV file and creates objects from its contents.

Export-CsvThis cmdlet takes an object (or objects) as input and create sa CSV file, as shown here:

PS>dir | Export-Csv c:\temp\file.csv

This simple example shows how to output the contents of the current directory to a CSV-formatted file. Looking at the contents of this file displays information about the objects, though, instead of just plain strings such as filenames.

The following example creates a simple CSV-formatted file and then is read in using the Import-Csv cmdlet. The Select-Object cmdlet is used to ...

Get Microsoft® SQL Server 2008 R2 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.