Using PowerShell to import data properly into an array -
i trying put powershell script read data csv (it tab separated , not csv so, we'll use commas) , import data powershell multidimensional array. csv has following data:
empid,name,dept 12345,john,service 56789,sarah,sales 98765,chris,director
my script follows:
$temp=gc "temp.csv" $array=@() $temp | foreach{ $elements=$_.split(",") $array+= ,@($elements[0],$elements[1],$elements[2]) } foreach($value in $array) { write-host "empid =" $value[0] "and name =" $value[1] "and dept =" $value[2] }
the above works, able refer array elements more proper name because there lot more elements shown above , gets confusing. this, i’ve tried use hash tables, defining new objects, members, types, etc. never seem work properly. may wrong feel i’m running in circle , may lost. basically, want is, instead of using:
write-host "empid =" $value[0] "and name =" $value[1] "and dept =" $value[2]
i replace above this:
write-host "empid =" $value.empid "and name =" $value.name "and dept =" $value.dept
is possible? if so, can point me in correct direction? csv coming source , comes header row anyway (it can’t removed except manually editing csv). use header row name array columns don’t have use it. primary question (goal) here @ least able name different columns. can done either dynamically using first row imported csv or entering names script itself.
thanks
you're working hard. import-csv work comma separation default, , handle tab delimited using -delimiter. there can reference columns name.
import-csv "temp.csv" | write-host "empid=" + $_.empid
Comments
Post a Comment