Search code examples
excelpowershellfile-format

Check Excel file format from PowerScript not relying on file extension


We have a number of Excel files that are stored in .xlsx or .xlsm format. Unfortunately the last letter of the file extension got dropped in another IT system so that they all have the file extension .xls. But we also have Excel files stored in .xls format.

I would need to write a PowerShell script to detect the actual Excel file format and then rename the files so that they file extension fits to the content. We have thousands of these files so it is not an option to it manually.

I found information on how to differentiate between .xls and the newer XML formats like .xlsx and .xlsm (by inspecting the first characters in the file). But I have not yet been able to find any information on how to determine if it is an .xlsx or .xlsm.

Any ideas?


Solution

  • I did a quick check on content type of a xlsx and xlsm files. FYI basically excel files are archives containing various information and content.

    What you can do is unzip both files and check content of file [Content_Types].xml

    XLSX has:

    <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>

    while XLSM has:

    <Override PartName="/xl/workbook.xml" ContentType="application/vnd.ms-excel.sheet.macroEnabled.main+xml"/>

    From this point you can go forward :)