Skip to content

Instantly share code, notes, and snippets.

@riskeez
Forked from gabceb/ConvertXLS.ps1
Last active January 25, 2022 10:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save riskeez/096f3ee6bc23d35ed7730bbd36b33c44 to your computer and use it in GitHub Desktop.
Save riskeez/096f3ee6bc23d35ed7730bbd36b33c44 to your computer and use it in GitHub Desktop.
Powershell script to convert all xls documents to xlsx in a folder recursively
# XLS to XLSX Batch convert script
# Forked from https://gist.github.com/gabceb/954418
# Works well using Office 365
$folderpath = "D:\XLS Files"
$filetype ="*xls"
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
write-host $xlFixedFormat
$excel = New-Object -ComObject excel.application
$excel.visible = $true
Get-ChildItem -Path $folderpath -Include $filetype -recurse |
ForEach-Object `
{
$path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
"Converting $path"
$workbook = $excel.workbooks.open($_.fullname)
$path += ".xlsx"
$workbook.saveas($path, $xlFixedFormat)
$workbook.close()
$oldFolder = $path.substring(0, $path.lastIndexOf("\")) + "\old"
write-host $oldFolder
if(-not (test-path $oldFolder))
{
new-item $oldFolder -type directory
}
move-item $_.fullname $oldFolder
}
$excel.Quit()
$excel = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
@madhuridara
Copy link

Thank You so much for the script.

The powershell script converts to xlsx but it has a pop-up message (Message Attached)

How can I say Continue Saving as a Macro-Free workbook?

ExcelFileError

Thank You,
Madhuri Dara

@madhuridara
Copy link

Ignore my comment above. I could get past the pop-up by adding this:

$excel.DisplayAlerts = $False

-Madhuri Dara

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment