Convert CSV to XLSX in PowerShell

Convert CSV to XLSX in PowerShell

The csv file can be easily converted into xlsx using the following methods:

Before converting files, please verify that your PowerShell is opened in administrative mode and that the execution policy is not set to Restricted.

The above output demonstrates that the execution policy is set to Restricted. So, if the current execution policy is set to Restricted, you can modify it to RemoteSigned by executing the following command. Note that there is a possibility that your execution policy was already set to RemoteSigned.

The change of the execution policy to RemoteSigned enables the conversion of XLSX files to CSV files using the methods given below.

Using PSExcel Module

To convert CSV to xlsx in PowerShell:

  • Import PSExcel module
  • Use Import-csvcmd to read csv and convert it to table like custom objects.
  • Pipe table like custom objects to Export-Excel to convert it to Excel File.

First, we imported the PSExcel module, which works with Excel files in PowerShell. After that, set the paths for the CSV and XLSX files to the variables csvPath and xlsxPath, respectively. Then, the Import-Csv read the data from the CSV file specified by $csvPath and stored the PowerShell variable $csv.

Next, the Export-Excel command converted the data (stored in the $csv variable) into an Excel file. The resulting Excel file is saved to the path specified by $xlsxPath. Next, the -AutoSize automatically adjusts the column widths to fit the data. Finally, the message on the screen indicates that the CSV file has been successfully converted to an XLSX file using the Write-Host command.

If the PSExcel module is not installed, install it using the Install-Module PSExcel command.

Using ImportExcel Module

To convert CSV to xlsx in PowerShell:

  • Import ImportExcel module
  • Use Import-csvcmd to read csv and convert it to table like custom objects.
  • Pipe table like custom objects to Export-Excel to convert it to Excel File.

First, we imported the ImportExcel module usig Import-Module cmdlet. We then loaded the data of the Excel file into a PowerShell variable named data using Import-CSV cmdlet. We assigned the path to the location of the csv file.

After that, we exported the data stored in the data variable to a xlxs file using Export-Excel cmdlet without including type information and also gave the file name along with the path where the file should be located. Then open the specified location where the new xlsx file is created as output.

Using Excel.Application COM Object

To convert CSV to xlsx in PowerShell:

  • Create object of type Excel.Application
  • Use open() method to read csv file into workbook.
  • Use SaveAs() method to save file as XLSX.

First, assign the path of the source CSV file and targeted xlsx file to PowerShell variables named $csvFile and $xlsxFile, respectively. After that, we created a new instance of the Excel.Application using the COM object. The instance is assigned to a variable named $excel.

The workbook is opened using this instance. The workbook is then saved as XLSX using the SaveAs function with two arguments. The first argument specifies the file’s location while the second argument, 51, indicates the file type, XLSX. Finally, the workbook is closed, and the Excel.Application instance is terminated.

That’s all about how to convert CSV to XLSX in PowerShell.

Was this post helpful?

Leave a Reply

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