Split a CSV into multiple files with PowerShell

How to split a CSV into multiple files with PowerShell by row count or column value, what the split changes in your data, and when to use a line-based approach.

PowerShell can split one CSV file into several smaller files without extra software. It can split by a fixed number of rows or by the value in a column, such as one file per region or per customer. This review covers when each approach fits, what the split actually changes in your data, and what to check before you share the output with people or import it into other tools.

Is splitting a CSV with PowerShell a good fit?

For most everyday files, yes. The built-in Import-Csv and Export-Csv cmdlets understand CSV structure. They handle quoted fields, embedded commas and line breaks inside quoted values, and they write the header row into every output file automatically. That correctness is the main benefit compared with splitting by raw lines.

The tradeoff is fidelity and resources. Import-Csv turns each row into an object, and Export-Csv writes new text from those objects. The output holds the same values, but it is not a byte-for-byte copy of the original. Quoting, encoding and line endings can differ. Approaches that load the whole file into memory first can also struggle with very large files.

A plain line-by-line split, which copies the original text unchanged, is preferable in two cases: when the files are very large, or when downstream systems are strict about exact formatting. It is only safe when no quoted field contains a line break. If you cannot confirm that, use the CSV-aware cmdlets and accept the reformatting.

What "split" can mean

Searches for splitting a CSV usually mean one of three things. Each needs a different script.

  • By row count: every output file holds up to a fixed number of data rows, for example to fit an upload limit.
  • By column value: rows are grouped by a field such as Region or Status, giving one file per distinct value.
  • By file size: output files stay under a byte limit. PowerShell has no built-in switch for this, so you would track bytes written yourself. This review does not cover it in detail.

What the split actually changes

Splitting changes the stored text, not just how it is displayed. Each output file is a new file. With the cmdlet approach, its contents are generated from parsed objects. The following differences are documented PowerShell behavior worth knowing before you rely on the result.

  • Header row: Export-Csv writes the column names as the first line of every file, taken from the object properties.
  • Type line: Windows PowerShell 5.1 adds a "#TYPE" line at the top unless you pass -NoTypeInformation. PowerShell 7 omits it by default, and the switch is accepted but no longer needed.
  • Quoting: Windows PowerShell 5.1 puts quotes around every field. PowerShell 7 also quotes all fields by default, but its -UseQuotes parameter lets you quote only when needed, or never.
  • Values as text: Import-Csv reads every value as a string. Leading zeros and long numeric IDs survive in the file itself. Whether they survive when someone opens the file in a spreadsheet depends on that application.
  • Encoding: default output encodings differ between Windows PowerShell 5.1 and PowerShell 7, which defaults to UTF-8 without a byte order mark. Set -Encoding explicitly so the result does not depend on which version runs the script.
  • Line endings: generated files use the platform's newline convention, which may differ from the source file.
  • Duplicate or blank headers: Import-Csv cannot map two columns with the same name to one object. Clean up the header row before splitting.

Split by a fixed number of rows

This version loads the whole file, then writes it out in slices. It is simple and CSV-aware, but memory use grows with file size. Adjust the path, the chunk size and the output name pattern to suit your file.

$rows = Import-Csv -Path .\data.csv
$size = 1000

for ($i = 0; $i -lt $rows.Count; $i += $size) {
    $part = [math]::Floor($i / $size) + 1
    $last = [math]::Min($i + $size, $rows.Count) - 1
    $rows[$i..$last] |
        Export-Csv -Path ".\data_part$part.csv" -NoTypeInformation -Encoding utf8
}

In Windows PowerShell 5.1, -Encoding utf8 writes a byte order mark. In PowerShell 7 it does not, and utf8BOM is available if a destination requires one. Some spreadsheet applications use the byte order mark to detect UTF-8, so check what your recipients' software expects.

Split by the value in a column

Group-Object collects rows that share a value. Each group can then be exported to its own file. Column values may contain characters that are not allowed in file names, so the script replaces those characters first.

Import-Csv -Path .\data.csv |
    Group-Object -Property Region |
    ForEach-Object {
        $name = $_.Name -replace '[\\/:*?"<>|]', '_'
        if (-not $name) { $name = 'blank' }
        $_.Group |
            Export-Csv -Path ".\Region_$name.csv" -NoTypeInformation -Encoding utf8
    }

Watch for three things. Group-Object holds every row in memory until grouping is finished. Values that differ only in letter case may be grouped together, because Group-Object is case-insensitive unless you add -CaseSensitive. On file systems that ignore case, two values that differ only in case would also map to the same file name.

Large files: a line-based split

For files too large to load comfortably, reading and writing lines with .NET stream classes keeps memory use flat. It also copies each line exactly as it appears in the source. The catch is that it treats every line break as the end of a row. If any quoted field contains a line break, that row will be cut in two, and the output will be corrupted without any warning.

$source = (Resolve-Path .\big.csv).Path
$size = 100000
$reader = [System.IO.StreamReader]::new($source)
$header = $reader.ReadLine()
$part = 0
$count = 0
$writer = $null

try {
    while ($null -ne ($line = $reader.ReadLine())) {
        if ($count % $size -eq 0) {
            if ($writer) { $writer.Dispose() }
            $part++
            $writer = [System.IO.StreamWriter]::new((Join-Path $PWD "big_part$part.csv"))
            $writer.WriteLine($header)
        }
        $writer.WriteLine($line)
        $count++
    }
}
finally {
    if ($writer) { $writer.Dispose() }
    $reader.Dispose()
}

The script passes full paths to the .NET classes because .NET's working directory can differ from PowerShell's current location. A middle option is also available. You can pipe Import-Csv directly to Export-Csv -Append, choosing the target file as you go. This stays CSV-aware without loading everything at once, but it is noticeably slower because the file is reopened for each write. No timing measurements were available for this review.

Will the split files stay usable after sharing?

CSV is plain text, so the split files copy, email and paste like any other text. Splitting does not reorder characters or alter the meaning of the content. Problems usually come from how the receiving software interprets the file, not from the split itself.

  • Encoding mismatches show up as garbled accented or non-Latin characters when the receiving tool assumes a different encoding.
  • Delimiter expectations vary by region. Some spreadsheet locales expect semicolons. Import-Csv and Export-Csv accept -Delimiter or -UseCulture to match.
  • Tab-separated files work the same way. Pass -Delimiter "`t" to both cmdlets and use a .tsv or .txt extension the destination recognizes.
  • Spreadsheet applications may reformat values such as long IDs, dates or numbers with leading zeros when they open a file. That happens in the application, not in the CSV.
  • For accessibility, a CSV has no table markup of its own. How well a screen reader conveys rows and columns depends on the application used to open it.

Behavior in specific destinations has not been tested for this review. That includes particular spreadsheet programs, database import tools and assistive technologies. Before a large batch goes out, open one output file in the actual destination and confirm that the headers, special characters and row counts look correct.

Quick checks after splitting

  • Add up the data rows across all output files and compare the total with the source, excluding repeated headers.
  • Confirm every file starts with the same header line.
  • Spot-check rows that contain commas, quotes or line breaks inside fields.
  • Confirm the encoding and delimiter match what the destination expects.