How to use the PowerShell ConvertFrom- CSV Cmdlet to Save Coding Time with PS Script

Home > Videos > PowerShell > How to use the PowerShell ConvertFrom- CSV Cmdlet to Save Coding Time with PS Script

How to use the PowerShell ConvertFrom- CSV Cmdlet to Save Coding Time with PS Script

Like This Video 0 126Jason Yoder
Added by March 2, 2018

In this video, PowerShell instructor Jason Yoder shows how to use the ConvertFrom-CSV PowerShell Cmdlet to easily convert standard CSV files into PowerShell objects and speed up coding time.

For instructor-led PowerShell training classes, see our course schedule:

Microsoft Windows PowerShell Training

PowerShell ConvertFrom-CSV script used in this video.

Download ConvertFrom-CSV PowerShell Script

 

Video Trascription

I’m Jason Yoder with Interface Technical Training. Today I’m going to show you a PowerShell cmdlet that might help you reduce the amount of coding that you need to do.

When we’re asking for information from our environments it’s usually from some controlled source. Maybe it’s Active Directory. Maybe we’re getting it from SharePoint. Maybe we’re getting it from Azure. We have control of this and we know what type of information we’re going to receive.

Other times you’re going to be asking for information from the Internet and it may not come in a format that works well with PowerShell. We have this saying, “Object in, Object out.” What that means is that we expect an object to be received by our PowerShell cmdlets.

Remember an object is a packet of data that contains readable information to the computer. We also expect object out, which means anything that we produce we want to make sure it makes an object so that the next cmdlet in the pipeline can accept it.

Let’s take a look at a little situation here. We’re going to go ahead and download some information from the Internet. As you can see, here’s where we’re receiving some stock information. I understand what the first two fields mean, but after that I have no idea really what the rest of this is trying to tell me, because there’s no headers to this.

I’m not really sure what kind of information I’m looking in. It looks comma separated to me. What I am going to do is I am going to ask for this information again and I’m going to store it in a variable called Data. When I display Data, there’s that information.

I am kind of questioning here, what can I do with this? What data type am I working with? I’m going to run the get type method against that variable and it tells me that I’m working with string. If you’ve worked with a string object before, you know there’s only one property in string and that’s called length. It’s the number of characters that it contains.

I’m interested in what those actual numbers are. I need to convert them some way to make it readable to PowerShell. In the past people would write a lot of different types of code in order to segment out that line.

For example, we’re going to go ahead and run the split method. I can clearly see the different elements of that string, but I still do not have any header information. I cannot let PowerShell read this.

What we’re going to do it we’re going to work with a convert from CSV cmdlet. I’m going to go ahead and open up its help file, and just draw your attention to the top. “Converts object properties in comma separated value format into CSV versions of the original object.” In layman’s terms it’s going to take this string and if we do things right it’s going to make it an object out of the CSV file.

Let’s see what happens. I’m just going to go ahead and pipe my information straight to it, and nothing. Something is not quite right here. Let’s take a look at one of the parameters in the help file for a convert to CSV. It’s called Header.

This is where this cmdlet’s going to shine for us because instead of us writing a bunch of code to populate different properties for this object, it’s going to do the work for us. Header specifies an alternate column header row for the imported string. The column header determines the names of the properties of the objects that convert from CSV creates.

You can see I’m using it here and I’m providing a name for each one of the values that’s inside of this CSV string. If we look, I’m going to have Symbol be the first property header. I’m going to have Name be the second, and so on and so forth. You do have to provide them in order. It has no idea what you’re talking about. You must have them in order.

Let’s go ahead and run this. Now you can see that we have a PowerShell object created. Over here on the right we have properties and over here on the left we have their values. Now we can feed this into the PowerShell pipeline and use this. This data’s in a format that a computer can understand.

Just to show you, I’m going to go ahead and run this again and pipe it to get Member. You can see that we have an object. We can now use it with PowerShell.

 

 

 

Videos You May Like

How to Build in a PSMethod to your PowerShell Code

0 42 0

In this video, PowerShell instructor Jason Yoder shows how to add Methods (PSMethod) to your code using free software that's added into the PSObject.

Subnetting a TCP/IP Network using the Magic Box Method

0 1084 3

In this session, we are going to look at how to subnet a Class B address into multiple network segments using what's called The Magic Box. Let's run through this real quick.

Detailed Forensic Investigation of Malware Infections – April 21, 2015

4 435 3

In this IT Security training video, Security expert Mike Danseglio (CISSP / CEH) will perform several malware investigations including rootkits, botnets, viruses, and browser toolbars.

Write a Comment

Share your thoughts...

Please fill out the comment form below to post a reply.