且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从CSV导入到哈希表,然后导出回CSV

更新时间:2022-12-31 11:08:09

  • 要将"CSV导入"转换为哈希表,请使用组对象".
  • 要将哈希表导出回CSV,必须使用.GetEnumerator()方法将其分解为与Export-Csv一起使用的对象.
  • 使用上面的示例:

$pathCsv = ($env:TEMP + '\test.csv')
Get-Process | Export-Csv $pathCsv

#import CSV to array as hash table using the ID property as the key
$toHashTable = Import-Csv $pathCsv | Group-Object -AsHashTable -Property ID

#make changes to hashtable here

#break the hashtable into the pipeline, then grab each $_.Value property
#and break that out of the hashtable into the pipeline
$toHashTable.GetEnumerator() | ForEach{($_.Value).GetEnumerator()} | Export-Csv $pathCsv

我在这里问了这个问题,所以我可以为正在寻求解决同一问题的其他任何人回答.

I asked this question here so i could answer it for anyone else who was searching for a resolution to the same problem.