Solved

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

Posted on 2016-11-16
7
15 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
  • 4
  • 3
7 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 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 83

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Synchronize a new Active Directory domain with an existing Office 365 tenant
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now