Powershell out-file csv to columns

Is there a way to use the out-file command and load data into a csv file so when opened into excel it shows data in multiple colums.

 

Example. Loop with three variables($a, $b, and $c). each time through the loop, the variables are loaded with different data. at the end of each loop, there is an out-file (with append) command that writes to a CSV and puts $a in column A, $b in column b, and $c in column c.

 

Is this possible?

November 2nd, 2011 4:54pm

Have you tried this?

Get-Help Export-Csv


Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2011 5:04pm

You can use Export-CSV as Rich points out, but to also answer your question about Out-File, below is an example of how you can use it to accomplish your goal.

"{0}`t{1}`t{2}" -f "ColumnA","ColumnB","ColumnC" | Out-File -FilePath "file.csv"
1..10 | ForEach {
    "{0}`t{1}`t{2}" -f $_,($_+1),($_+2) | Out-File -FilePath "file.csv" -Append
}

November 2nd, 2011 5:15pm

you can't append with export-csv, at least, not as far as I can see. so i wouldn't be able to use it in the loop.

Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2011 5:33pm

You can append to a .csv with Dmitry's function:

http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/

You can also try my method on http://unlockpowershell.wordpress.com/2009/12/15/powershell-export-csv-with-no-header/

Karl

November 2nd, 2011 5:42pm

You can append to a .csv with Dmitry's function:

http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/

You can also try my method on http://unlockpowershell.wordpress.com/2009/12/15/powershell-export-csv-with-no-header/

Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2011 6:09pm

you can't append with export-csv, at least, not as far as I can see. so i wouldn't be able to use it in the loop.

The usual course that most people take is to store the objects in a variable and when the loop is done, pipe the variable out to Export-Csv.
November 2nd, 2011 6:33pm

take a look here also Chris

http://www.lucd.info/2010/05/29/beyond-export-csv-export-xls/

 

Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2011 12:19am

I know this is really old but I found this page when I was searching for a similar solution and here is what I found:

To make sure the data gets in separate columns when you try to open it in excel, make sure you use the parameter "-encoding ascii" for the out-file part of the script

so it would look something like

"format your 3 variables as required here" | convertto-csv -notypeinformation | select skip 1 | out-file -encoding ascii -append $filename

October 31st, 2013 9:36pm

I know this is really old but I found this page when I was searching for a similar solution and here is what I found:

To make sure the data gets in separate columns when you try to open it in excel, make sure you use the parameter "-encoding ascii" for the out-file part of the script

so it would look something like

"format your 3 variables as required here" | convertto-csv -notypeinformation | select skip 1 | out-file -encoding ascii -append $filename

Just use Export-Csv. It has an -Encoding parameter and the default is already ascii.

http://technet.microsoft.com/en-us/library/hh849932%28v=wps.620%29.aspx

Free Windows Admin Tool Kit Click here and download it now
October 31st, 2013 10:58pm

I know this is really old but I found this page when I was searching for a similar solution and here is what I found:

To make sure the data gets in separate columns when you try to open it in excel, make sure you use the parameter "-encoding ascii" for the out-file part of the script

so it would look something like

"format your 3 variables as required here" | convertto-csv -notypeinformation | select skip 1 | out-file -encoding ascii -append $filename

This is what helped me the most. All these looping answers aren't the best solution in my case. The CSV file I was writing is so huge that writing it all at the end is doomed to fail.
June 30th, 2015 12:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics