Import CSV Into Excel With PowerShell

How-to basics · Updated November 3, 2026

Import CSV Into Excel With PowerShell

To import a CSV file and create a real Excel workbook, use PowerShell’s Import-Csv with the ImportExcel module, then pipe the rows to Export-Excel. If Microsoft Excel is installed on a Windows computer, you can instead automate Excel with COM; the best method depends on whether you need Excel itself to open the file or simply need an .xlsx output.

What PowerShell Does With a CSV

A CSV file is plain text: each line represents a row, and separators divide the fields. It is not an Excel workbook, so it cannot store worksheet formatting, multiple sheets, charts, or Excel formulas as workbook features. PowerShell’s Import-Csv reads the first row as column headings by default and turns subsequent rows into objects whose properties are those headings.

That distinction matters: Import-Csv alone does not open Excel or create an .xlsx file. It reads data into PowerShell. To create a workbook, send those objects to a tool that can write the Excel format, such as the ImportExcel module, or use Excel’s own Windows automation interface. You can also leave the data as CSV if your only goal is to open it in Excel; Excel can open CSV files directly, but the file remains a CSV unless you save it as an Excel workbook.

Use the ImportExcel Module for Most Scripts

The ImportExcel module is a PowerShell Gallery module that can read and write Excel workbooks without requiring desktop Excel to be installed. It is often the simpler choice for scheduled scripts and repeatable file conversions. Install it for your user account, then import the CSV and export the resulting objects:

Install-Module ImportExcel -Scope CurrentUser

$csvPath = 'C:\Data\sales.csv'
$xlsxPath = 'C:\Data\sales.xlsx'

Import-Csv -LiteralPath $csvPath |
    Export-Excel -Path $xlsxPath -WorksheetName 'Sales' -AutoSize -FreezeTopRow -BoldTopRow

The command expects a comma-delimited CSV with a header row. It writes the data to a worksheet named Sales, sizes columns to their contents, freezes the header row, and formats that row in bold. The output is an .xlsx workbook, not a renamed CSV. Choose an output path that does not accidentally point to the input file, and decide how your script should handle an existing workbook before running it repeatedly.

Installing a module may require permission to contact the PowerShell Gallery, and an organization may require an approved repository or software review. Check your organization’s policy before installing modules. The module is separate from PowerShell itself; it is not a Microsoft Excel feature.

Set the Delimiter and Handle Missing Headers

Comma is the default delimiter for Import-Csv, but not every CSV uses commas. Files produced in some regional settings use semicolons, and tab-separated text needs a tab delimiter. Set the delimiter to match the actual file rather than assuming its name tells you how fields are separated:

Import-Csv -LiteralPath 'C:\Data\europe-sales.csv' -Delimiter ';' |
    Export-Excel -Path 'C:\Data\europe-sales.xlsx' -WorksheetName 'Sales'

For a tab-separated file, use -Delimiter "`t". PowerShell handles quoted fields, including a comma inside a quoted value, when it parses a properly formed CSV. Avoid splitting each line yourself with a simple comma operation: that can break names or descriptions that contain quoted commas.

If the file has no header row, provide column names with Import-Csv -Header. For example, Import-Csv -LiteralPath 'C:\Data\raw.csv' -Header 'Date','Item','Amount' assigns names to the fields; when headers are supplied this way, the first line is treated as data rather than as a header. Check that the number and order of names match the file’s columns.

Choose Between ImportExcel and Excel COM

Use ImportExcel when you need a dependable .xlsx file but do not need to launch the Excel application. It suits command-line conversion, scheduled jobs, and machines that do not have Office installed. It can also create worksheets and apply workbook formatting, though its available behavior depends on the module version.

Excel COM automation tells the installed Windows Excel application to open and save a file. It can be useful when you specifically need Excel’s own file-opening behavior or a feature that depends on the desktop application. It requires Excel on that Windows computer and is generally a poor fit for unattended server-side automation. Microsoft does not recommend automating Office applications in non-interactive server scenarios because dialogs, user profiles, and application state can make jobs unreliable.

PowerShell COM automation is not a way to control Excel for the web, and it is not the equivalent workflow on Mac. Excel for the web does not expose the Windows COM interface. For cross-platform scripts, a module such as ImportExcel is usually the more practical starting point.

Convert a CSV by Opening It in Excel

On Windows with desktop Excel installed, this example asks Excel to open the CSV and save it as an Excel workbook. The number 51 is Excel’s file-format code for an .xlsx workbook:

$csvPath = 'C:\Data\sales.csv'
$xlsxPath = 'C:\Data\sales.xlsx'
$excel = $null
$workbook = $null

try {
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $false
    $excel.DisplayAlerts = $false
    $workbook = $excel.Workbooks.Open($csvPath)
    $workbook.SaveAs($xlsxPath, 51)
}
finally {
    if ($workbook) {
        $workbook.Close($false)
        [void][Runtime.InteropServices.Marshal]::ReleaseComObject($workbook)
    }
    if ($excel) {
        $excel.Quit()
        [void][Runtime.InteropServices.Marshal]::ReleaseComObject($excel)
    }
    [GC]::Collect()
    [GC]::WaitForPendingFinalizers()
}

The finally block closes the workbook and Excel application even if an error occurs, and releases the COM references to reduce the chance of a leftover Excel process. Test the script on a copy before using it on important files. Excel may interpret delimiters and values according to regional settings, and its automatic type detection can change how data appears. If delimiter handling must be precise, parse the CSV with Import-Csv using the known delimiter, then write the resulting data with ImportExcel.

Protect Identifiers, Dates, and Other Values

CSV has no built-in cell types. A value such as 001247 is just text in the file, but Excel may interpret it as a number and display 1247. Long numeric identifiers can also be converted to scientific notation or lose precision if treated as numbers. Postal codes, account references, and product codes are usually identifiers, not quantities; preserve them as text and inspect the resulting workbook.

Dates can also be ambiguous. A date such as 03/04/2026 could mean March 4 or April 3 depending on the expected convention. If consistency matters, use an unambiguous source format such as year-month-day, or explicitly parse and format dates in your PowerShell process before exporting. Do not assume that a value which looks like a date in the CSV will be interpreted identically on every computer.

Check the first row, column order, and representative values after conversion. If the source contains untrusted text, be alert to values beginning with characters Excel can treat as formulas, such as an equals sign. Do not blindly open or distribute untrusted CSV data as if it were harmless display text; use a controlled import process and verify how potentially dangerous values are represented.

Common Errors and Practical Checks

  • Wrong columns or one long column: Confirm the delimiter. A semicolon-delimited file read with the comma default will not split into the intended fields.
  • Unexpected column names: Check that the CSV begins with the expected header row and that headers do not contain unexpected spaces or duplicate names.
  • Garbled characters: The file’s text encoding may not match the import settings. PowerShell versions differ in their default encoding behavior; inspect the source encoding and use an explicit encoding option supported by the PowerShell version in use.
  • Leading zeros disappear: Excel has interpreted an identifier as a number. Preserve that column as text and check the saved workbook rather than relying on the CSV’s appearance in a text editor.
  • Access denied or file in use: Close the output workbook in Excel, check folder permissions, and make sure the script is not writing to a protected location.
  • Workbook is too large: An Excel worksheet supports up to 1,048,576 rows and 16,384 columns. Very large data sets may need to be filtered, split across files, or handled in a database or another data tool.

When to Use Excel’s Import Interface Instead

For a one-time import, using the Excel interface may be easier than writing a script. In Excel for Microsoft 365 on Windows, choose Data > Get Data > From File > From Text/CSV, select the file, and review the preview before loading it. The preview lets you check the delimiter and data interpretation before the values enter the sheet. Menu labels and available options can vary by platform and update; Excel for Mac does not use Windows COM automation.

Use a PowerShell script when the same operation needs to run repeatedly, when you need consistent output paths, or when the conversion is part of a larger data workflow. Use the interface when you want to inspect an unfamiliar file and make import decisions interactively. In either case, converting to .xlsx does not magically repair bad source data: confirm that columns and values are correct before relying on the workbook.

Frequently Asked Questions

Can PowerShell import a CSV directly into an open Excel workbook?
Import-Csv reads data into PowerShell; it does not place data in an open workbook by itself. Use ImportExcel to write a workbook file, or automate the installed Excel application with COM if you need to interact with desktop Excel on Windows.

Do I need Microsoft Excel installed to create an .xlsx file?
No, not if you use the ImportExcel module. Excel must be installed for the COM example because that method controls the Windows desktop Excel application.

How do I import a semicolon-separated CSV?
Pass -Delimiter ';' to Import-Csv, then send the objects to your chosen Excel-writing method. The delimiter must match the file’s actual structure.

Does changing a CSV file’s extension to .xlsx convert it?
No. Renaming changes only the filename, not the file format. Use ImportExcel or save the opened CSV from Excel as an Excel workbook.

Why did Excel remove zeros at the start of a value?
Excel likely treated the value as a number. CSV does not record a column’s intended type, so import or conversion can trigger automatic interpretation. Keep identifier columns as text and verify the output.

Can I run the Excel COM script on Mac or Excel for the web?
No. The example uses Windows COM automation and a locally installed desktop Excel application. For Mac or web-based workflows, use a compatible data tool or create the workbook with a cross-platform PowerShell module instead.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.