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

Categories

0 votes
1.6k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...