Convert XLS to CSV Using PowerShell – Windows Automation Guide (2024)

Convert XLS to CSV with PowerShell – Scripts for Windows Automation

Introduction

Windows system administrators and IT professionals often need to automate XLS to CSV conversion without installing third-party software. PowerShell, which is built into Windows, can do this natively using the Excel COM object — no extra software needed.

Prerequisites

You need Microsoft Excel installed on the same machine. PowerShell 5.1+ (included in Windows 10/11) is required.

Method 1: Convert Single XLS File to CSV

# Convert a single XLS/XLSX file to CSV

$InputPath  = ‘C:\Data\report.xlsx’

$OutputPath = ‘C:\Data\report.csv’

$Excel = New-Object -ComObject Excel.Application

$Excel.Visible = $false

$Excel.DisplayAlerts = $false

$Workbook = $Excel.Workbooks.Open($InputPath)

$Workbook.SaveAs($OutputPath, 6)  # 6 = xlCSV format

$Workbook.Close($false)

$Excel.Quit()

[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel) | Out-Null

[System.GC]::Collect()

Write-Host ‘Conversion complete: ‘ $OutputPath

Method 2: Batch Convert All XLS Files in a Folder

$InputFolder  = ‘C:\Excel_Files’

$OutputFolder = ‘C:\CSV_Output’

if (-not (Test-Path $OutputFolder)) {

    New-Item -ItemType Directory -Path $OutputFolder | Out-Null

}

$Excel = New-Object -ComObject Excel.Application

$Excel.Visible = $false

$Excel.DisplayAlerts = $false

Get-ChildItem -Path $InputFolder -Filter ‘*.xls*’ | ForEach-Object {

    $CsvName = [System.IO.Path]::GetFileNameWithoutExtension($_.Name) + ‘.csv’

    $CsvPath = Join-Path $OutputFolder $CsvName

    $Workbook = $Excel.Workbooks.Open($_.FullName)

    $Workbook.SaveAs($CsvPath, 6)

    $Workbook.Close($false)

    Write-Host “Converted: $($_.Name) -> $CsvName”

}

$Excel.Quit()

[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel) | Out-Null

Write-Host ‘All files converted successfully!’

Method 3: Schedule Automatic XLS to CSV Conversion

You can schedule the PowerShell script to run automatically using Windows Task Scheduler:

  1. Save your PowerShell script as Convert-XLS.ps1
  2. Open Task Scheduler (search in Start Menu).
  3. Click ‘Create Basic Task’.
  4. Set the trigger (Daily, Weekly, or When a file changes).
  5. Set Action to ‘Start a program’: powershell.exe
  6. Add arguments: -ExecutionPolicy Bypass -File C:\Scripts\Convert-XLS.ps1
  7. Click Finish to save the scheduled task.

PowerShell vs Python for XLS Conversion

Use PowerShell when: You are on Windows, Excel is installed, you need a simple scheduled task, and you do not want to install Python. Use Python when: You need cross-platform support, you are processing very large files, or you need complex data transformation during conversion.

Frequently Asked Questions (FAQs)

Do I need Excel installed to convert XLS with PowerShell?

Yes. The PowerShell COM method requires Microsoft Excel to be installed. If you do not have Excel, consider using Python with pandas which works without Excel on any operating system.

Can I run PowerShell XLS conversion on a server?

Only if Microsoft Excel is installed on that server. This is common in Windows Server environments. For Linux servers, use Python or PHP instead.

How do I fix the ‘ExecutionPolicy’ error when running the script?

Run PowerShell as Administrator and execute: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser. This allows local scripts to run while keeping security for downloaded scripts.

Internal Linking Suggestions

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top