如何使用 Export-Csv 将 powershell 的哈希表导出到 CSV 文件?

发布于 2024-10-18 06:16:38 字数 1138 浏览 5 评论 0原文

如何使用 Export-Csv 将 powershell 的哈希表导出到 CSV 文件?

PS C:\>$UsersAndGroups
Name                           Value                                                                                                                                                             
----                           -----                                                                                                                                                             
user1                          {Group2}                                                                                                                    
user2                          {Group1, Group2, Group3}                                                                                                                                   
user3                          {Group3, Group4}     

我想将其放入 CSV 文件中,其中每个键值对都位于新行中。第一列应始终是键,下一列应是值。

就像这样:

|col1    col2    col3    col4   
 +------------------------------  
1|User1  Group2  
2|User2  Group1  Group2  Group3 
3|User3  Group3  Group4

非常感谢。

How to export powershell's hashtable into CSV file using Export-Csv ?

PS C:\>$UsersAndGroups
Name                           Value                                                                                                                                                             
----                           -----                                                                                                                                                             
user1                          {Group2}                                                                                                                    
user2                          {Group1, Group2, Group3}                                                                                                                                   
user3                          {Group3, Group4}     

I want to get this into CSV file where each key-value pair is in new line. First column should allways be key and next columns should be values.

Like this:

|col1    col2    col3    col4   
 +------------------------------  
1|User1  Group2  
2|User2  Group1  Group2  Group3 
3|User3  Group3  Group4

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-10-25 06:16:38

尝试这样的事情:

PS> $ht = @{user1=,'Group2';user2='Group1','Group2','Group3';
            user3='Group3','Group4'}

PS> $ht.GetEnumerator() | 
     Foreach {$obj = new-object psobject -prop @{col1=$_.Name}; $_.Value | 
        Foreach {$i=2} `
                {Add-Member NoteProperty "col$i" $_ -Inp $obj; $i++} {$obj} } | 
     Sort {$_.psobject.properties.count} -desc | ConvertTo-Csv -NoTypeInformation

"col1","col2","col3","col4"
"user2","Group1","Group2","Group3"
"user1","Group2",,
"user3","Group3","Group4",

Try something like this:

PS> $ht = @{user1=,'Group2';user2='Group1','Group2','Group3';
            user3='Group3','Group4'}

PS> $ht.GetEnumerator() | 
     Foreach {$obj = new-object psobject -prop @{col1=$_.Name}; $_.Value | 
        Foreach {$i=2} `
                {Add-Member NoteProperty "col$i" $_ -Inp $obj; $i++} {$obj} } | 
     Sort {$_.psobject.properties.count} -desc | ConvertTo-Csv -NoTypeInformation

"col1","col2","col3","col4"
"user2","Group1","Group2","Group3"
"user1","Group2",,
"user3","Group3","Group4",
千寻… 2024-10-25 06:16:38

单程:

 $usersandgroups = @{
 user1 = @("group2")
 user2 = @("group1","group2","group3")
 user3 = @("groupt3","group4")
 }

 $exp = @("col1,col2,col3,col4")

 $usersandgroups.keys |%{
 $exp += (@($_) + $usersandgroups.$_) -join ","
 }
 $exp
 $exp  | out-file test.csv
 $imp = import-csv test.csv
 $imp | ft -auto

 col1,col2,col3,col4
 user3,groupt3,group4
 user1,group2
 user2,group1,group2,group3

 col1  col2    col3   col4  
 ----  ----    ----   ----  
 user3 groupt3 group4       
 user1 group2               
 user2 group1  group2 group3

One way:

 $usersandgroups = @{
 user1 = @("group2")
 user2 = @("group1","group2","group3")
 user3 = @("groupt3","group4")
 }

 $exp = @("col1,col2,col3,col4")

 $usersandgroups.keys |%{
 $exp += (@($_) + $usersandgroups.$_) -join ","
 }
 $exp
 $exp  | out-file test.csv
 $imp = import-csv test.csv
 $imp | ft -auto

 col1,col2,col3,col4
 user3,groupt3,group4
 user1,group2
 user2,group1,group2,group3

 col1  col2    col3   col4  
 ----  ----    ----   ----  
 user3 groupt3 group4       
 user1 group2               
 user2 group1  group2 group3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文