Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

powershell - Export csv spits out length only

I can only get the length when exporting this to csv, how should it be done properly.

$redo = Import-CSV c:empestimport.txt | Group-Object email |
foreach {  "{0} ,{1}" -f $_.Name, (($_.Group | foreach { $_.group }) -join ', ')

}

$redo | Export-CSV c:empest.csv -NoTypeInformation
#

"Length" "46" "59" "110" "47" "149" "38" "69" "32" "62" "29" "49" "31" "27" "48" "55" "42"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Export-Csv expects an object (or a list of objects) with properties, whereas your command pipeline produces an array of strings. If you feed this array into Export-Csv the cmdlet takes the properties of each given item (which is only Length for strings) and writes those properties to the output file.

You need to build a list of objects with the desired properties instead, e.g.:

Import-CSV c:empestimport.txt `
  | Group-Object email `
  | select @{n="Name";e={$_.Name}},@{n="Group";e={($_.Group | %{$_.group}) -join ', '}} `
  | Export-CSV c:empest.csv -NoTypeInformation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...