Solved

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

Posted on 2016-11-16
7
23 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

22 Experts available now in Live!

Get 1:1 Help Now