#Requires -Version 5.1 <# JbTecWiz Support Centre -- generated fix script Fault : A document will not open -- Protected View, "file format is not valid", or a corrupt file Fix : Recover a file that is genuinely damaged Source: https://jbtecwiz.com/support/win-office-file-wont-open Run as : The Office application, with a copy of the file Expect : 40 minutes Risk : medium Reversible : yes WHEN THIS IS THE RIGHT FIX The first three have been ruled out. Assume the file matters and work on a copy throughout. HOW TO UNDO IT The original file was never modified -- discard the working copy and start again if a recovery attempt made things worse. This script walks the fix one step at a time and asks before each one. Steps with no command are things you do yourself -- it prints those and waits. Run with -DryRun to print without executing. #> [CmdletBinding()] param( # Print every step and command without running anything. [switch]$DryRun, # Do not ask before each step. Read the script first if you use this. [switch]$Unattended ) $ErrorActionPreference = 'Stop' $script:Failed = 0 # - THIS SCRIPT WILL NOT RUN UNTIL YOU EDIT IT - # The commands below contain values only you can supply. Search the # script for each one, replace it, then delete this block. $script:MustEdit = @( 'C:\path\to\file.xlsx' # a placeholder file path ) if ($script:MustEdit.Count) { Write-Host "" Write-Host ' This script needs editing before it can run.' -ForegroundColor Yellow Write-Host ' Replace each of these with a real value:' -ForegroundColor Yellow $script:MustEdit | ForEach-Object { Write-Host (" " + $_) -ForegroundColor Yellow } Write-Host "" Write-Host ' Then delete the $script:MustEdit block near the top.' Write-Host "" exit 2 } function Write-Rule { param([string]$Text) Write-Host '' Write-Host ('-' * 70) -ForegroundColor DarkGray if ($Text) { Write-Host $Text -ForegroundColor Cyan } } function Show-Prose { param([string]$Text, [string]$Colour = "Gray") if (-not $Text) { return } $words = $Text -split "\s+"; $line = " " foreach ($w in $words) { if (($line.Length + $w.Length) -gt 74) { Write-Host $line -ForegroundColor $Colour; $line = " " } $line += "$w " } if ($line.Trim()) { Write-Host $line -ForegroundColor $Colour } } function Invoke-Step { param( [int]$Number, [string]$Do, [string]$Why, [scriptblock]$Command, [switch]$Manual, [string]$Shell = "powershell" ) Write-Rule " Step $Number of 5" Show-Prose $Do "White" if ($Why) { Write-Host ""; Show-Prose $Why "DarkGray" } if ($Manual) { Write-Host '' Write-Host ' -> Do this yourself, then press Enter to carry on.' -ForegroundColor Yellow if (-not $Unattended -and -not $DryRun) { [void](Read-Host) } return } Write-Host '' foreach ($l in ($Command.ToString().Trim() -split "`n")) { Write-Host (" " + $l.Trim()) -ForegroundColor Green } Write-Host '' if ($DryRun) { Write-Host " (dry run -- not executed)" -ForegroundColor DarkGray; return } if (-not $Unattended) { $a = Read-Host " Run this step? [Y]es / [S]kip / [Q]uit" if ($a -match "^[Qq]") { Write-Host " Stopped at your request."; exit 0 } if ($a -match "^[Ss]") { Write-Host " Skipped." -ForegroundColor DarkGray; return } } try { & $Command } catch { $script:Failed++ Write-Host (" Step $Number failed: " + $_.Exception.Message) -ForegroundColor Red Show-Prose "The rest of the fix may depend on this. Read the write-up before carrying on." "Red" if (-not $Unattended) { $c = Read-Host " Carry on anyway? [y/N]" if ($c -notmatch "^[Yy]") { exit 1 } } } } Write-Rule Write-Host ' A document will not open -- Protected View, "file format is not valid", or a corrupt file' -ForegroundColor White Write-Host ' Recover a file that is genuinely damaged' -ForegroundColor Cyan Write-Host '' Write-Host ' Risk: medium Reversible 40 minutes' Write-Rule if (-not $Unattended -and -not $DryRun) { $go = Read-Host ' Ready? [y/N]' if ($go -notmatch "^[Yy]") { Write-Host " Nothing was changed."; exit 0 } } Invoke-Step -Number 1 -Do 'Copy the file somewhere local before doing anything to it, and keep the original untouched.' -Why 'Every recovery route below can make things worse. The original is the only thing that guarantees you can start again.' -Command { Copy-Item 'C:\path\to\file.xlsx' "$env:USERPROFILE\Desktop\file-working-copy.xlsx" } Invoke-Step -Number 2 -Do 'Use Open and Repair: File -> Open -> Browse, select the file, then click the arrow beside the Open button and choose Open and Repair. Try Repair first, then Extract Data if that fails.' -Manual Invoke-Step -Number 3 -Do 'If the file is a modern format, it is a zip -- check whether it is structurally intact.' -Why 'If this lists xl/workbook.xml and the sheets, the container is fine and the damage is inside one part -- often recoverable by opening the file in LibreOffice, which is more tolerant of malformed XML, and re-saving it.' -Command { Add-Type -AssemblyName System.IO.Compression.FileSystem [IO.Compression.ZipFile]::OpenRead("$env:USERPROFILE\Desktop\file-working-copy.xlsx").Entries | Select-Object FullName, Length } Invoke-Step -Number 4 -Do 'Look for an AutoRecover copy and for previous versions, both of which are frequently faster than any repair.' -Command { Get-ChildItem "$env:APPDATA\Microsoft\Excel" -Recurse -Include *.xlsb,*.xlsx -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 10 FullName, LastWriteTime } Invoke-Step -Number 5 -Do 'If the file lives on a share or in OneDrive, check version history there before spending any more time on repair -- it very often has a clean copy from an hour earlier.' -Manual Write-Rule " Confirm it worked" Show-Prose 'The recovered file opens, the data is complete, and it re-saves without a warning.' 'White' Write-Rule if ($script:Failed -gt 0) { Write-Host (" Finished with " + $script:Failed + " failed step(s).") -ForegroundColor Yellow Show-Prose 'Read the full write-up at https://jbtecwiz.com/support/win-office-file-wont-open' 'Yellow' } else { Write-Host ' Finished.' -ForegroundColor Green } Write-Host '' Show-Prose 'To undo: The original file was never modified -- discard the working copy and start again if a recovery attempt made things worse.' 'DarkGray' Write-Rule