Creating and appending CSV from powershell
I am trying to create a csv file from powershell with the following headers "A,B,C,D"
Under each header, I wish to append data to the csv file after it is created.
I'm currently using
$csv = "Path,Publish,Hashlist,Package`r`n"
$fso = new-object -comobject scripting.filesystemobject
$file = $fso.Cre开发者_Go百科ateTextFile($path,$true)
$file.write($csv)
$file.close()
to create the csv and trying to append it with
Add-Content browser.csv "$filepath, $publishtimestamp, $hashdestination, $packagedestination"
However, when appending the .csv file I've created here, it doesn't work correctly and looks like an encoding error. If I manually create the csv with those headers and then append, it works fine. Any ideas on the correct way to create a csv for my purposes?
If you are seeing encoding issues, use the -Encoding
flag of Out-File
and Add-Content
to specify the encoding you want (UTF8 etc.) Why are you not using Powershell cmdlets for this? Powershell provides easier ways to write to a file, append to a file etc. Not to mention that there is the Export-Csv
cmdlet for easily manipulating csv.
精彩评论