Friday 18 May 2012

importing data from a csv file with powershell


This is the contents of my data.csv file:

Col1,Col2,Col3
jack,jack@test.com,1234567
john,john@test.com,7654321

Here is the powershell code of my ImportFromCSV.ps1:

$dataSource=import-csv "data.csv"
foreach($dataRecord in $datasource)
{
    $name=$dataRecord.Col1
    $email=$dataRecord.Col2
    $phone=$dataRecord.Col3
    write-host $name $email $phone
}

Running the code gives the following output:

jack jack@test.com 1234567
john john@test.com 7654321

For this to work data.csv and ImportFromCSV.ps1 should be in the same directory

No comments:

Post a Comment