Convert Tab delimited txt file to .csv in powershell

Posted on 2011-03-01
Last Modified: 2012-05-11
I need to convert a text file to .csv using powershell. The column headers should be

Name     Path        Full      Incr

I would like it sorted by first column name.

Please provide exact code
Question by:LouisSanchez
  • 2
  • 2
LVL 27

Expert Comment

ID: 35013332
Are the headers in the text file? if so you can try this

$in = import-csv c:\import.txt -Delimiter `t
$in | Sort Name | export-csv c:\out.csv

Author Comment

ID: 35013507
Sorry headers aren't in txt file
LVL 27

Accepted Solution

KenMcF earned 500 total points
ID: 35013613
There are probably better ways to do this but see if this works for you

$test = gc c:\temp\2.txt
$u = @()
$u += "Name `t Path `t Full `t Incr"
$test | %{$u += $_}
ConvertFrom-Csv -inputobject $u -delimiter `t | export-csv c:\temp\testout.csv -notypeinformation

Open in new window


Author Closing Comment

ID: 35018781
Worked awesome. Thanks

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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.
A brief introduction to what I consider to be the best editor for PowerShell.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

831 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