Solved

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

Posted on 2016-11-16
7
37 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 84

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 84

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
Does Powershell have you tied up in knots?

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

 

Author Comment

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

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
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…

733 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