#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 : Check the File Block policy Source: https://jbtecwiz.com/support/win-office-file-wont-open Run as : PowerShell as the affected user Expect : 20 minutes Risk : medium Reversible : yes WHEN THIS IS THE RIGHT FIX Old formats fail across the board -- every .xls or every .doc -- rather than one particular file. These blocks exist because the legacy binary formats have a long history of parser vulnerabilities: unblock the narrowest format that solves the problem, convert the file, and put the block back. HOW TO UNDO IT Set the value back to 2, or delete the value you added, to restore the block. 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 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 ' Check the File Block policy' -ForegroundColor Cyan Write-Host '' Write-Host ' Risk: medium Reversible 20 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 'Read the current File Block settings for the application.' -Why 'A value of 2 blocks the format outright. 1 opens it in Protected View. 0 permits it. Baselines such as the Microsoft security baseline set several of these to 2, which is a deliberate hardening choice someone made, not a defect.' -Command { Get-ItemProperty 'HKCU:\Software\Microsoft\Office\16.0\Excel\Security\FileBlock' -ErrorAction SilentlyContinue } Invoke-Step -Number 2 -Do 'Also check the policy hive, which overrides the user one and cannot be changed by the user.' -Command { Get-ItemProperty 'HKCU:\Software\Policies\Microsoft\Office\16.0\Excel\Security\FileBlock' -ErrorAction SilentlyContinue } Invoke-Step -Number 3 -Do 'Decide whether the format should be permitted at all. The right answer for a one-off archive file is usually to convert it on a controlled machine, not to unblock the format for everyone.' -Manual Invoke-Step -Number 4 -Do 'If it is to be permitted, set the specific format to open in Protected View rather than fully trusting it.' -Command { Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Office\16.0\Excel\Security\FileBlock' -Name 'Excel95Workbooks' -Type DWord -Value 1 } Invoke-Step -Number 5 -Do 'Once the file is open, save it to a current format so the problem does not recur.' -Manual Write-Rule " Confirm it worked" Show-Prose 'The file opens, in Protected View if that is what you set, and re-saves cleanly as .xlsx or .docx.' '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: Set the value back to 2, or delete the value you added, to restore the block.' 'DarkGray' Write-Rule