Creating a Script to Convert Excel to CSV, with additional goals

Hey guys, I’m looking for some help with my current project for my company. Any resources are welcome, but I’m also open to feedback or input on my code.

I’m a fairly new developer and have just started to learn PowerShell so a lot of this is new to me. If i’m doing things totally wrong, please let me know.

Here’s my code and scope of the project.


SET dir="%~dp0"
Powershell -Command "\FILELOCATION\XLSX-to-CSV.ps1 Convert-CsvInBatch -folderPath \"dir\""
<#main script
Requires EXCEL to be installed on the machine
Script will require a path location for the input files
.Expected Outputs
Script will export all .XLSX files in a given directory or given selected files to .CSV files in "X Directory(s)"
#Modify XLSX Document | Open each EXCEL Object | Save as .CSV File
Function Convert-CsvInBatch
#Get all .XLSX files in Directory
$ExcelFiles = Get-ChildItem -Path $Folder -Filter *.xlsx -Recurse
#Creates Excel COMObject
$excelApp = New-Object -ComObject Excel.Application
$excelApp.DisplayAlerts = $false
#For Each File, Delete the Top Row
$ExcelFiles | ForEach-Object {
$workbook = $excelApp.Workbooks.Open($_.FullName)
$csvFilePath= $_.FullName-replace"\.xlsx$",".csv"
#Count # of Sheets in Workbook
while ($p-le$i) {
$workbook.SaveAs($csvFilePath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSV)
# Release Excel Com Object resource
$excelApp.Visible = $true
Start-Sleep 5
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApp) | Out-Null

Updated the code to contain my latest iteration.

Great. Are you facing any problem with this code ? To convert Excel to CSV you can take help of ImportExcel module.

Unfortunately the goal is to avoid the use of modules and have few dependencies so the script can be automated. I’ve make some changes to the scope and updates to the code, once I test I’ll post a new update on my process.

Thank you for your input though!

Just out of curiosity, why?

It would be ONE dependency and of course you still would be able to automate whatever you want!? I do understand why someone wouldn’t like to have Excel installed only to be able to create an Excel sheet but the module ImportExcel is less than 25 MB … so no big deal at all. And it would make your life much much easier I think. Why inventing the wheel again and again? :wink: :smiley: At least try it.

Hey guys, thank you for the help.

I’ve finished the script and gotten it running.

The reason we had to avoid the use of modules was importing them on the organizations systems/servers is difficult due to access restrictions.

Had to change the scope a little for the teams requirements, but it’s my first successful PS endeavour.

Thank you!

Hi, is the code above updated to reflect the working version? I have a script that does virtually the same thing, but started generating errors after a February .NET update. Wondering if your solution will work with the updates applied?