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
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:
PowerShell ConvertFrom-CSV script used in this video.
Download ConvertFrom-CSV PowerShell Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <# ╔══════════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ Using ConvertFrom-CSV ║ ╟──────────────────────────────────────────────────────────────────────────────╢ ║ Interface Technical Training ║ ║ Jason A Yoder ║ ║ ║ ╚══════════════════════════════════════════════════════════════════════════════╝ #> # Get information from the internet. $Sym = "Msft" $URL = "http://finance.yahoo.com/d/quotes.csv?s=$Sym&f=snl1ghjkvw4P2" Invoke-RestMethod -Uri $URL # Save the output into a variable. $Data = Invoke-RestMethod -Uri $URL # Display the contents of the variable. $Data # Get the data type. $Data.GetType() # Using the Split method of System.String does not help very much. $Data.Split(",") # Look at the help file for ConvertFrom-CSV Get-Help ConvertFrom-CSV -ShowWindow # Convert the string to a CSV. $Data | ConvertFrom-csv # Use ConvertFrom-CSV Header parameter. Get-Help ConvertFrom-CSV -Parameter Header $Data | ConvertFrom-csv -Header Symbol,Name,Ask,Low,High,Low52,High52,Volume,DayChange,ChangePercent # Verify that an object has been created. $Data | ConvertFrom-csv -Header Symbol,Name,Ask,Low,High,Low52,High52,Volume,DayChange,ChangePercent | Get-Member |
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.
You May Also Like
ConvertFrom Cmdlet, ConvertFrom-CSV, CSV cmdlet, PowerShell Cmdlets, PowerShell Objects
A Simple Introduction to Cisco CML2
0 3804 0Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2
Cable Testers and How to Use them in Network Environments
0 694 1This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader demonstrates how to use cable testers in network environments. Let’s look at some tools that we can use to test our different cables in our environment. Cable Testers Properly Wired Connectivity … Continue reading Cable Testers and How to Use them in Network Environments
How to Build in a PSMethod to your PowerShell Code
0 71 0In this video, PowerShell instructor Jason Yoder shows how to add Methods (PSMethod) to your code using free software that’s added into the PSObject. For instructor-led PowerShell courses, see our course schedule. Microsoft Windows PowerShell Training Download the Building Methods PowerShell script</a> used in this video. <# ╔══════════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ Building Methods ║ ╟──────────────────────────────────────────────────────────────────────────────╢ … Continue reading How to Build in a PSMethod to your PowerShell Code