PowerShell tutorial

Convert Text File to CSV in PowerShell

Use PowerShell to convert text files to CSV on Windows: re-delimit with Import-Csv and Export-Csv, build records from whitespace-separated text, set UTF-8 encoding, and loop over many files.

Step by stepCopy-paste codeFree converter included
Prefer not to code? Use the text to CSV converter — it runs in your browser, free, with nothing uploaded.

Open the converter

Advertisement
Overview

Convert text to CSV from the PowerShell prompt

PowerShell ships with everything needed to convert text to CSV on Windows, and its object pipeline makes it easy to reshape data on the way. The examples below cover the common cases: changing a file’s delimiter, parsing plain text into fields, and processing a whole folder.

For a single file you can also use the online converter, but PowerShell shines for scheduled tasks and bulk jobs.

Re-delimit a file with Import-Csv / Export-Csv

If your text file already has a header row and a known delimiter, convert it to comma CSV in one pipeline. The backtick-t (`t) is PowerShell’s tab character:

PowerShell — tab-delimited text to CSV
Import-Csv -Path "input.txt" -Delimiter "`t" |
    Export-Csv -Path "output.csv" -NoTypeInformation -Encoding UTF8

Build records from whitespace-separated text

When the text has no header and is separated by spaces, split each line and project it into named columns:

PowerShell — whitespace text to CSV with headers
Get-Content "input.txt" | ForEach-Object {
    $parts = $_ -split '\s+'
    [PSCustomObject]@{
        Name  = $parts[0]
        Email = $parts[1]
        Age   = $parts[2]
    }
} | Export-Csv "output.csv" -NoTypeInformation -Encoding UTF8

Batch-convert every .txt in a folder

Loop over each text file and write a CSV beside it, keeping the original base name:

PowerShell — convert many files
Get-ChildItem -Filter *.txt | ForEach-Object {
    Import-Csv $_.FullName -Delimiter "`t" |
        Export-Csv ($_.BaseName + ".csv") -NoTypeInformation -Encoding UTF8
}

For dozens of files at once without scripting, the batch converter handles up to 50 in the browser.

Good to know

Common pitfalls and verifying the output

PowerShell’s convenience hides a few sharp edges. The first is encoding: in Windows PowerShell 5.1, -Encoding UTF8 writes a byte-order mark, which helps Excel but can trip up Unix tools; in PowerShell 7 the same switch writes UTF-8 without a BOM, and you use UTF8BOM when you want one. If accented characters look wrong downstream, the encoding switch is almost always the cause.

The second is the #TYPE header. Windows PowerShell 5.1 prepends a type line unless you add -NoTypeInformation; PowerShell 7 omits it by default. Forgetting the switch leaves a stray first line that breaks naive parsers. The third is that Import-Csv keys columns by the header row and reads every value as a string — ideal for re-delimiting, but a file without a header needs -Header supplied, or the first record is consumed as column names.

To verify a conversion, pipe the result back in and inspect it: Import-Csv output.csv | Select-Object -First 3 | Format-Table shows the parsed columns, confirming delimiter and quoting. When looping over many files, write each output beside its source and test on copies first — a mistaken path in a ForEach-Object block can overwrite originals.

If your source is genuinely unstructured — log lines, key-value pairs, irregular spacing — Import-Csv is the wrong tool; instead read it with Get-Content, parse each line with a regular expression or -split, and build a [PSCustomObject] per record before piping to Export-Csv. That pattern handles almost any text shape while keeping the object pipeline working end to end.

In short, for files that already have headers and a known delimiter, the one-line Import-Csv to Export-Csv pipeline is all you need; for everything else, parse into objects first. Either way, add the encoding switch your downstream tool expects and you have a conversion you can schedule and forget. That reliability is exactly what makes PowerShell well suited to unattended, scheduled conversions on Windows servers and workstations.

FAQ

Convert Text File to CSV in PowerShell — FAQ

How do I convert a tab-delimited file in PowerShell?

Use Import-Csv -Delimiter "`t" piped to Export-Csv -NoTypeInformation.

How do I set the encoding?

Add -Encoding UTF8 to Export-Csv. In PowerShell 7, use UTF8BOM if Excel needs a BOM.

How do I process many files?

Pipe Get-ChildItem into a ForEach-Object loop and Export-Csv each file.

Why does Export-Csv add a #TYPE line?

Add -NoTypeInformation (Windows PowerShell 5) to suppress it; PowerShell 7 omits it by default.