Using PowerShell to import data properly into an array

SOSidb

I am trying to put together a PowerShell script that will read data from a CSV (it is actually tab separated and not CSV but for SO, we'll use commas) and import that data into a PowerShell multidimensional array. My CSV has the following data:

EmpID,Name,Dept
12345,John,Service
56789,Sarah,Sales
98765,Chris,Director

My script is as 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, but I would like is to be able to refer to the array elements by a more proper name because there are a lot more elements than are shown above and this gets confusing. To do this, I’ve tried to use hash tables, defining new objects, members, types, etc. but they never seem to work properly. I may be wrong but I feel like I’m running in a circle and now I may be lost. Basically, what I want to do is, instead of using:

write-host "EmpID =" $value[0] "and Name =" $value[1] "and Dept =" $value[2]

I would like to replace the above with something like this:

write-host "EmpID =" $value.EmpID "and Name =" $value.Name "and Dept =" $value.Dept

Is this possible? If so, can someone point me in the correct direction? The CSV is coming from another source and comes with the header row anyway (it can’t be removed except by manually editing the CSV). So I would like to use the header row to name the array columns but I don’t have to use it. My primary question (goal) here is to at least be able to name the different columns. It can be done either dynamically by using the first row imported from the CSV or by entering the names into the script itself.

Thanks

Tim Ferrill

You're working too hard. Import-CSV will work with comma separation by default, and you could handle tab delimited using -Delimiter. From there you can reference the columns by name.

Import-CSV "temp.csv" | Write-Host "EmpID=" + $_.EmpID

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Django: ajax POST sending data of array of objects not working properly

来自分类Dev

How to import data from MySql into Hive using Apache Nifi?

来自分类Dev

How can I retrieve data from array list properly for the first action

来自分类Dev

How to use an array of objects properly

来自分类Dev

How to import data by csv in bulk from files in one directory using Cypher in Neo4j?

来自分类Dev

Using Javascript to sort table properly?

来自分类Dev

How to append array data to spreadsheet column at once using Google Script?

来自分类Dev

Parsing xml using powershell

来自分类Dev

Array.Powershell阵列查找

来自分类Dev

配置MSBuild以启动PowerShell并运行Import-Module $ {TargetFileName)

来自分类Dev

使用PowerShell和Import-CSV foreach循环处理变量

来自分类Dev

具有Import-Module的Powershell运行脚本

来自分类Dev

PowerShell-Import-Csv-按列位置引用列

来自分类Dev

Powershell import-csv-自动识别定界符?

来自分类Dev

Powershell import-csv $ variable正确为空

来自分类Dev

Powershell:import-csv和get-aduser

来自分类Dev

配置MSBuild以启动PowerShell并运行Import-Module $ {TargetFileName)

来自分类Dev

PowerShell-Import-Csv-按列位置引用列

来自分类Dev

运行 import-csv powershell 脚本时 CSV 的字符限制

来自分类Dev

Powershell 脚本无法使用 Import-Clixml 命令运行

来自分类Dev

MySQL LOAD DATA INFILE limits my import?

来自分类Dev

Multiple CSVs to import into R with missing key data

来自分类Dev

Using powershell to read html content

来自分类Dev

2-dimensional array not set properly in matlab to C++ conversion

来自分类Dev

用于循环和比较的Powershell Array方法?

来自分类Dev

why data method not working properly while getting ID?

来自分类Dev

使用System.Data.SqlDBType varbinary的Powershell

来自分类Dev

How to properly apply a patch with moved/renamed files using Git

来自分类Dev

array :: data和array :: front的用法

Related 相关文章

  1. 1

    Django: ajax POST sending data of array of objects not working properly

  2. 2

    How to import data from MySql into Hive using Apache Nifi?

  3. 3

    How can I retrieve data from array list properly for the first action

  4. 4

    How to use an array of objects properly

  5. 5

    How to import data by csv in bulk from files in one directory using Cypher in Neo4j?

  6. 6

    Using Javascript to sort table properly?

  7. 7

    How to append array data to spreadsheet column at once using Google Script?

  8. 8

    Parsing xml using powershell

  9. 9

    Array.Powershell阵列查找

  10. 10

    配置MSBuild以启动PowerShell并运行Import-Module $ {TargetFileName)

  11. 11

    使用PowerShell和Import-CSV foreach循环处理变量

  12. 12

    具有Import-Module的Powershell运行脚本

  13. 13

    PowerShell-Import-Csv-按列位置引用列

  14. 14

    Powershell import-csv-自动识别定界符?

  15. 15

    Powershell import-csv $ variable正确为空

  16. 16

    Powershell:import-csv和get-aduser

  17. 17

    配置MSBuild以启动PowerShell并运行Import-Module $ {TargetFileName)

  18. 18

    PowerShell-Import-Csv-按列位置引用列

  19. 19

    运行 import-csv powershell 脚本时 CSV 的字符限制

  20. 20

    Powershell 脚本无法使用 Import-Clixml 命令运行

  21. 21

    MySQL LOAD DATA INFILE limits my import?

  22. 22

    Multiple CSVs to import into R with missing key data

  23. 23

    Using powershell to read html content

  24. 24

    2-dimensional array not set properly in matlab to C++ conversion

  25. 25

    用于循环和比较的Powershell Array方法?

  26. 26

    why data method not working properly while getting ID?

  27. 27

    使用System.Data.SqlDBType varbinary的Powershell

  28. 28

    How to properly apply a patch with moved/renamed files using Git

  29. 29

    array :: data和array :: front的用法

热门标签

归档