Convert Tab delimited txt file to .csv in powershell

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
Who is Participating?
KenMcFConnect With a Mentor Commented:
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

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
LouisSanchezAuthor Commented:
Sorry headers aren't in txt file
LouisSanchezAuthor Commented:
Worked awesome. Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.