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.
|
1 2 3 |
Get-ExecutionPolicy |
|
1 2 3 |
Set-ExecutionPolicy 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.
|
1 2 3 4 |
Set-ExecutionPolicy RemoteSigned Get-ExecutionPolicy |
|
1 2 3 |
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.
|
1 2 3 4 5 6 7 8 |
Import-Module PSExcel $csvPath = "C:\path\to\file.csv" $xlsxPath = "C:\path\to\output.xlsx" $csv = Import-Csv $csvPath $csv | Export-Excel -Path $xlsxPath -AutoSize Write-Host "CSV file successfully converted to XLSX 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
PSExcelmodule is not installed, install it using theInstall-Module PSExcelcommand.
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.
|
1 2 3 4 5 |
Import-Module -Name ImportExcel $data = Import-Csv -Path "C:\path\to\file.csv" $data | Export-Excel -Path "C:\path\to\output.xlsx" -NoHeader |
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 asXLSX.
|
1 2 3 4 5 6 7 8 9 |
$csvFile = "C:\path\to\file.csv" $xlsxFile = "C:\path\to\output.xlsx" $excel = New-Object -ComObject Excel.Application $workbook = $excel.Workbooks.Open($csvFile) $workbook.SaveAs($xlsxFile, 51) $workbook.Close() $excel.Quit() |
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.