Convert XLSX to CSV in PowerShell

This article will discuss converting Excel Files (XLSX) to CSV using Windows PowerShell. We will see how to convert a single XLSX file, an Excel file with multiple worksheets, and, lastly, work through an example that shows how to convert all XLSX files into a folder to CSV.

The commands we will use in this post can be executed directly from PowerShell or inside a PowerShell script (a file with the extension .ps1). We will, however, be going with the second option in this article.

Converting a Single XLSX File into CSV

To demonstrate concepts in this Section, we will use the XLSX file saved as SalaryData2.xlsx with two sheets: Incomes and PersonalDetails.

Figure 1: A snapshot of the SalaryData2.xlsx file will be used in the examples. The file has two worksheets: Incomes and PersonalDetails.

The XLSX file can be converted into CSV in PowerShell by following these steps:

Step 0: Create a PowerShell Script to execute to perform the conversion

Save the following contents into a PowerShell script called xlsx_to_csv.ps1 (you can give it any other name).

Examples of FilePath and DestPath:

  • FilePathC:\Users\kiprono\Desktop\SalaryData2.xlsx
  • DestPathC:\Users\kiprono\Desktop\SalaryData2.csv

The ConvertExcelToCsv function takes two parameters: FilePath and DestPath. FilePath is the full path to the XLSX file, and DestPath is the path where the resulting CSV file is saved. The two parameters must be given.

Step 1: Execute the PowerShell Script

To execute the script created in step 0, run the following command on the PowerShell:

For example: cd C:\Users\<username>\Desktop ; .\xlsx_to_csv2.ps1

The line above executes two commands separated by a semi-colon (;). The first part changes the working directory to the script folder, and the second part executes the actual script.

Alternatively, if you have PowerShell 2.0, use PowerShell.exe’s -File parameter to invoke a script from another environment, like Command Prompt (cmd.exe). For example, you can run something like this on CMD.

Example: Powershell.exe -File cd C:\Users\kiprono\Desktop\xlsx_to_csv.ps1

Step 2: Looking at the output

Once you have executed the script, you should be able to find the CSV file in the location defined by the DestPath parameter. In my case, the output file, SalaryData2.csv, has the following truncated content (opened on notepad editor)


The output shows that SalaryData2.csv is truly CSV – a Comma Separated Values file.

Converting All Excel Files in a Folder into CSV

In this Section, we want to modify the code on the script above to suit the purpose. Let’s save the modified code to xlsx_to_csv3.ps1. Here is the new code

Once you have saved the above content into a PowerShell script named xlsx_to_csv3.ps1 (or anything else really), proceed to execute it as follows

For example: cd C:\Users\kiprono\Desktop ; .\xlsx_to_csv3.ps1

Note: The last two sections discussed converting a single worksheet of an XLSX file into CSV. In the next Section, let’s see how to convert XLSX with multiple sheets.

Converting XLSX with Multiple Worksheets to CSV

The following Section will modify the script contents in the second Section in a new file, xlsx_to_csv2.ps1. Replace the lines between the line $workbook = $Excel.Workbooks.Open($SourceFile) and $Excel.Quit() with the following contents and execute it as we did before.

The above content will allow us to loop through all the worksheets in an XLSX file and convert them to CSV. The new CSV file(s) generated will contain the XLSX filename, followed by an underscore (_), then the worksheet name.

For example, consider the SalaryData2.xlsx file introduced at the beginning of the article. It has two worksheets: Incomes and PersonalDetails.

In such a case, the modifications in this Section will result in two CSV files with the following names SalaryData2_Incomes.csv and SalaryData2_ PersonalDetails.csv.