PowerShell Problem Solver: Importing CSV Files Revisited

Posted on January 28, 2015 by Jeff Hicks in PowerShell with 0 Comments

In my previous article, I guided you through the potential pitfalls of importing a CSV file in PowerShell. If everything in your CSV file can be treated as a string, then you shouldn’t have any issues. But it’s more than likely that you will have some properties that need to be treated as integers or dates.

As promised, there’s another technique, albeit a little advanced. This is a great technique for when you’re inserting the same type of CSV file repeatedly. Let’s go back to my testdata.csv file.

Let’s say I get a new version of this file daily and want to do something with it in PowerShell. Reverting back to Import-CSV, I can see that everything is a string.

Using Import-CSV in Windows PowerShell. (Image Credit: Jeff Hicks)

Using Import-CSV in Windows PowerShell. (Image Credit: Jeff Hicks)

 

But I need the properties to be treated like this:

  • [DateTime] Date
  • [int32] Key
  • [String] Name
  • [String] Service
  • [Int32] Size
  • [Boolean] Test

What I need to do is to create a custom type extension that defines these properties. Starting in PowerShell 3.0, this became very easy to do using the Update-TypeData cmdlet.

The Update-TypeData cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

The Update-TypeData cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

 

Normally you would need to create a special XML file with all of the type extension information. But now we can do it dynamically and on-the-fly. To begin with, I’ll need to define a name for my custom class.

Right now, when I import test data everything is a PSCustomObject. I’ll show you how to change that before we are through.

Sponsored

I will add a new member, or property, to my custom class for each property I need to change. However, there is a problem. I can’t use the existing property value with a new name. That is to say a command like this won’t do what you think it will.

Instead, I have to define a new property name.

Because I need to invoke some methods, I will create a ScriptProperty. Use $this in the scriptblock instead of $_ to reference the current object being processed. I’ll repeat this for all of the other properties I want to customize.

Next, I am going to define a default set of display properties so that these new properties are used by default.

Some of the properties I’m not changing so I’ll use the original name. Using Get-TypeData I can see my changes.

Using Get-TypeData with Windows PowerShell. (Image Credit: Jeff Hicks)

Using Get-TypeData with Windows PowerShell. (Image Credit: Jeff Hicks)

 

Finally, I need to import my data from the CSV file and assign it a new type.

Piping to Get-Member, you can see the new properties.

The pipe to Get-Member shows us the new properties. (Image Credit: Jeff Hicks)

The pipe to Get-Member shows us the new properties. (Image Credit: Jeff Hicks)

 

And because I defined a set of default property names, that is what I see.

As long as I reference my new properties, I can easily filter and sort because they are already formatted as the proper type.

Filtering and sorting the properties in Windows PowerShell. (Image Credit: Jeff Hicks)

Filtering and sorting the properties in Windows PowerShell. (Image Credit: Jeff Hicks)

 

There is another way to configure all of this, especially if the header in your CSV isn’t what you want to use anyway. Let’s remove the custom type and try again.

My header looks like this: “Date”,”Name”,”Service”,”Key”,”Size”,”Test” but I can substitute in any header I want, although I need to read in CSV data that lacks a header. Instead of using Import-CSV, I’ll use ConvertFrom-CSV.

Using ConvertFrom-CSV in Windows PowerShell. (Image Credit: Jeff Hicks)

Using ConvertFrom-CSV in Windows PowerShell. (Image Credit: Jeff Hicks)

 

I show this to you so that you’ll understand this revised code:

I’ve essentially recreated my original CS header, but now the property values will be typed. Let’s re-import the data.

The “junk” property names are still part of the object.

Sponsored

But when it comes to working with the object, I can use my new properties.

I can use the properties when working with the object. (Image Credit: Jeff Hicks)

I can use the properties when working with the object. (Image Credit: Jeff Hicks)

 

You only have to create the type definition once. You could put it in your PowerShell profile or make it part of the script or function you write to import the data. You can use –Force to override any existing property names and avoid errors. Or use Get-TypeData to test if your custom type has already been added.

I’ve given you several techniques for importing CSV data and getting the values properly typed. You’ll have to decide what makes the most sense for your management or automation task.

Sponsored

Tagged with , ,