0

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.CreateTextFile($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?

3
  • 4
    I would highly, highly suggest looking at the Export-CSV cmdlet. You are re-inventing the wheel here. Commented Aug 15, 2011 at 20:07
  • If I wanted to create the CSV initially with just the headers in the top line, would you be able to explain how to do that with Export-CSV? I'm not too familiar with creating an object for export with only one line of content Commented Aug 18, 2011 at 23:10
  • Iguess that I would question the need to do this. It could be done, but it would be a little hacky. Instead why not just let Export-CSV create the headers the first time that you actually write data out? Commented Aug 19, 2011 at 13:15

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.