Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I combine small CSV files that share the same headers?

Quick answer

In PowerShell, list the exact CSV paths you want to combine, read each with Import-Csv, and compare the column names and order before collecting its rows. If a header differs, stop instead of merging. After the checks pass, export all rows to a separate path with Export-Csv. This example assumes every selected file has at least one data row; try it on copies first.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a few small CSV files with the same columns. What’s a safe way to combine them in PowerShell?

Geez

Select the files explicitly rather than relying on similar filenames or a broad wildcard. Compare each file’s actual column names, in order, before combining its rows. Import-Csv uses the header row to name each row object’s properties, while Export-Csv writes a header and values to a CSV file. [S1] [S2]

Here is a small example. Replace the bracketed paths with your own. It assumes every input has at least one data row and that the files can be read with compatible CSV formatting.

$files = @(
    '[folder]\part-1.csv',
    '[folder]\part-2.csv'
)
$output = '[folder]\combined.csv'

$allRows = @()
$expectedHeaders = $null

foreach ($file in $files) {
    $rows = @(Import-Csv -LiteralPath $file)

    if ($rows.Count -eq 0) {
        throw "No data rows found in $file; check the file before combining."
    }

    $headers = @($rows[0].PSObject.Properties.Name)

    if ($null -eq $expectedHeaders) {
        $expectedHeaders = $headers
    } elseif ([string]::Join([char]0, $headers) -cne
               [string]::Join([char]0, $expectedHeaders)) {
        throw "Headers differ in $file; nothing was exported."
    }

    $allRows += $rows
}

$inputPaths = @($files | ForEach-Object {
    [System.IO.Path]::GetFullPath($_)
})
$outputPath = [System.IO.Path]::GetFullPath($output)
if ($inputPaths -contains $outputPath) {
    throw 'Choose an output path that is different from every input path.'
}

$allRows | Export-Csv -LiteralPath $output -NoTypeInformation

The output-path check helps prevent replacing a selected input. The header check is strict: both names and order must match. Export-Csv uses the first object’s properties for the output layout, so checking before export helps avoid mismatched properties producing nulls or being omitted. [S2]

Follow-up question

Why check the headers instead of just combining files whose names look alike?

Geez

A filename does not establish that two files have the same columns. The check reads the property names created from each CSV’s header row, then stops if a later file’s names or order differ. [S1] That matters because PowerShell’s export layout is based on the first object’s properties; later objects with mismatched properties can produce nulls or have properties omitted. [S2]

For example, name,city and city,name contain the same names but in a different order. This example rejects that difference rather than treating it as a match. Likewise, name,city and name,region should not be combined as though they have the same columns. Review or correct the inputs first, then run the check again.

Follow-up question

What does the example assume, and what should I check before I run it?

Geez

It assumes each selected file has a header and at least one data row, and that the inputs can be read with compatible CSV formatting. The script gets header names from the first imported row, so a header-only file triggers the no-data check rather than being accepted. It also does not decide whether values from different files mean the same thing; matching column names alone cannot establish that.

The PowerShell documentation describes how Import-Csv and Export-Csv handle headers and objects. It does not establish that your files have compatible meanings or validate this complete multi-file workflow. [S1] [S2] Try the script on copies and inspect the resulting file before relying on it.

Follow-up question

Can you give me a quick reusable checklist for this?

Geez

Use this before and after each combine:

  1. List the exact input paths and confirm each file is one you intend to include.
  2. Check that each file has a header and at least one data row for this example.
  3. Compare header names and order; stop if any differ.
  4. Choose an output path that is not one of the input paths.
  5. Try the script on copies, then open the output and check its header and a few rows.

If a file has no data rows or the columns differ, review it separately rather than forcing it into the merge. The script does not validate the meaning or correctness of cell values.

A practical checklist

  • List the exact input paths and confirm each selected file is intended.

  • Confirm every input has a header and at least one data row for this example.

  • Compare column names and their order; stop if they differ.

  • Choose an output path that is not an input path.

  • Try the script on copies and inspect the output header and sample rows.

A few more useful answers

Will the script combine files with the same columns in a different order?

No. It compares header names in order and stops if the order differs. Review or reorder the columns before combining.

Can I use this example with a header-only CSV?

Not as written: it treats a file with no data rows as an error. Review that file separately or adapt the header-reading step before using it.

Does a matching header prove the files’ values are compatible?

No. Matching names and order only check the structure used here; they do not establish that the values have the same meaning or are correct.

Sources

References used for this guide.

  1. [S1] Import-Csv (Microsoft.PowerShell.Utility) — PowerShell 7.6
    learn.microsoft.com · Checked
  2. [S2] Export-Csv (Microsoft.PowerShell.Utility) — PowerShell 7.5
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work