Friday, October 11, 2013
Dealing With CSV Files
Let's take a look at some csv directly from Microsoft:
Name,Department,Title
Pilar Ackerman,Research,Manager
Jonathan Haas,Finance,Finance Specialist
Ken Myer,Finance,Accountant
Pop that into notepad and then open it up in Powershell using the below code:
$csvFile = "C:\Users\Administrator\Desktop\minor.csv"
Import-CSV $csvFile |
foreach-object
{
write-host $_.Title
}
Should give me Manager, Finance Specialist and Accountant. Wrong!
As it turns out, | Doesn't work in V3 of Powershell and removing it produces:
Name Department Title
---- ---------- -----
Pilar Ackerman Research Manager
Jonathan Haas Finance Finance Specialist
Ken Myer Finance Accountant
The following code works to create what used to work in Powershell V2.
$csvFile = "C:\Users\Administrator\Desktop\minor.csv"
$file_data = @(Import-CSV -Path $csvFile)
foreach($obj in $file_data)
{
$loc = $obj.Name
Write-host $loc
}
This will produce:
Pilar Ackerman
Jonathan Haas
Ken Myer
Subscribe to:
Posts (Atom)