?
Solved

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

Posted on 2016-11-16
7
Medium Priority
?
68 Views
Last Modified: 2016-11-16
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!
0
Comment
Question by:cmoerbe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41890105
The result will not be a "real" csv - the first line has one column, the second one multiple ones, so it's not a header line anymore.
That said, you convert it like this:
((Get-Content "C:\Temp\Input.txt") -join ',').Split(',', 2) | Set-Content "C:\Temp\Output.txt"

Open in new window

0
 

Author Comment

by:cmoerbe
ID: 41890176
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,
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41890195
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.
0
Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

 

Author Comment

by:cmoerbe
ID: 41890207
actually its not including commas at all i see
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41890215
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".
0
 

Author Comment

by:cmoerbe
ID: 41890544
Your code worked as expected. Thank you for the help!
0
 

Author Closing Comment

by:cmoerbe
ID: 41890545
Thanks for the advice!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question