Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-11-16
7
Medium Priority
?
87 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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