24/7/365 Support

Import Structured Data from a CSV File

Problem

You want to import structured data that has been stored in a CSV file. This is helpful when you want to use structured data created by another program, or structured data modified by a person.

Solution

Use PowerShell’s ImportCsv cmdlet to import structured data from a CSV file.

For example, imagine that you previously exported an inventory of the patches applied to a preVista system by KB number:

cd $env:WINDIR GetChildItem KB*.log | ExportCsv c:\temp\patch_log.csv

Somebody reviewed the CSV, and kept only lines from patch logs that they would like to review further. You would like to copy those actual patch logs to a directory so that you can share them.

PS >ImportCsv C:\temp\patch_log_reviewed.csv | ForeachObject { >> CopyItem –LiteralPath $_.FullName –Destination c:\temp\sharedlogs\ } >>

Discussion

As mentioned the CSV file format is one of the most common formats for exchanging semistructured data between programs and systems.

PowerShell’s ImportCsv cmdlet provides an easy way to import semistructured data to the PowerShell environment from other programs. When PowerShell imports your data from the CSV, it creates a new object for each row in the CSV. For each object, PowerShell creates properties on the object from the values of the columns in the CSV.

The preceding solution uses the ForeachObject cmdlet to pass each object to the CopyItem cmdlet. For each item, it uses the incoming object’s FullName property as the source path, and uses c:\temp\

sharedlogs\ as the destination. However, the CSV includes a PSPath property that represents the source, and most cmdlets support PSPath as an alternative (alias) parameter name for –LiteralPath. Because of this, we could have also written

PS >ImportCsv C:\temp\patch_log_reviewed.csv | >> CopyItem Destination c:\temp\sharedlogs\ >>

One thing to keep in mind is that the CSV file format supports only plain strings for property values. When you import data from a CSV, properties that look like dates will still only be strings. Properties that look like numbers will only be strings. Properties that look like any sort of rich data type will only be strings. That means that sorting on any property will always be an alphabetical sort, which is usually not the same as the sorting rules for the rich data types that the property might look like.

If your ultimate goal is to load rich unmodified data from something that you’ve previously exported from PowerShell, the ImportCliXml cmdlet provides a much better alternative.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say