Link to home
Start Free TrialLog in
Avatar of cmoerbe
cmoerbeFlag for United States of America

asked on

Add commas to text file and save as .csv using Powershell

I am trying take a text file that looks like this:

Column
Item 1
Item 2
Item 3

and make it look like this in .CSV format

Column,
Item1,Item2,Item3

When I try CovertTo-CSV I get output such as:

#Type System.string
"Length"
"4"
"8"
"7"
etc.

Can someone please advise?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cmoerbe

ASKER

Ok I see what you did. So if i wanted to keep it as a true .csv file would i need to do the split something like this? .Split(",")

Which I assume would keep all the data in one column under the header?

Column1,
Item1,
Item2,
Item3,
Avatar of oBdA
oBdA

Sorry, can't follow you.
Based on your example, the text file you have seems to be a csv already - a header line and some rows with items. It's a csv with only one column, but a csv nevertheless.
Avatar of cmoerbe

ASKER

actually its not including commas at all i see
With only one column, there are no fields to separate, hence no field separator. If there were a comma at the end, it would indicate a second column without data in it, and a missing column header.
Just run an "Import-Csv" on the file content you posted - you'll get an array of objects with one property, "Column".
Avatar of cmoerbe

ASKER

Your code worked as expected. Thank you for the help!
Avatar of cmoerbe

ASKER

Thanks for the advice!