Export csv spits out length only

后端 未结 3 2046
既然无缘
既然无缘 2020-12-21 14:40

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

$redo = Import-CSV c:\\temp\\testimport.txt | Group-Object email |
fore         


        
3条回答
  •  死守一世寂寞
    2020-12-21 15:07

    This was exactly what I was looking for. So I'm just going to add, my comment to be sure everyone understands.


    This does NOT work:

    $str_list = @('Mark','Henry','John')
    $str_list | Export-Csv .\ExportStrList.csv -NoType
    

    Because Export-Csv takes Objects and outputs properties. The only properties for a String[ ] is Length, so the CSV file only contains Lengths.

    To fix this, like the last guy said, we need to change the String[ ] into an Object[ ]. The simplest way is with Select-Object.


    Put each String into the Name property of a new Object[ ], like this:

    $str_list = @('Mark','Henry','John')
    $str_list = $str_list | Select-Object @{Name='Name';Expression={$_}}
    $str_list | Export-Csv .\ExportStrList.csv -NoType
    

    Just to re-iterate, Select-Object outputs a custom PSObject that can easily be manipulated. This is very powerful information, use it wisely.

提交回复
热议问题