#Requires -Version 5.1 <# JbTecWiz Support Centre -- generated fix script Fault : Printing from Office fails, prints blank, or goes to the wrong printer Fix : Pin the default printer and stop Windows moving it Source: https://jbtecwiz.com/support/win-office-printing Run as : PowerShell as the affected user Expect : 15 minutes Risk : low Reversible : yes WHEN THIS IS THE RIGHT FIX Jobs go somewhere unexpected, or Office claims there is no printer. HOW TO UNDO IT Set LegacyDefaultPrinterMode back to 0 to let Windows manage the default again. 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 = @( 'Your Printer Name' # a placeholder printer name ) 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 6" 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 ' Printing from Office fails, prints blank, or goes to the wrong printer' -ForegroundColor White Write-Host ' Pin the default printer and stop Windows moving it' -ForegroundColor Cyan Write-Host '' Write-Host ' Risk: low Reversible 15 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 'See what Windows currently thinks the default is.' -Command { Get-CimInstance Win32_Printer | Where-Object Default -eq $true | Select-Object Name, PortName, WorkOffline } Invoke-Step -Number 2 -Do 'Check whether Windows is managing the default itself.' -Why '"Let Windows manage my default printer" sets the default to whatever was last printed to, including at a different site. This is the answer whenever the default keeps changing on its own, and nothing in Office hints at it.' -Command { Get-ItemProperty 'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows' -Name LegacyDefaultPrinterMode -ErrorAction SilentlyContinue } Invoke-Step -Number 3 -Do 'Turn it off so the default stays where you put it.' -Command { Set-ItemProperty 'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows' -Name LegacyDefaultPrinterMode -Type DWord -Value 1 } Invoke-Step -Number 4 -Do 'Set the correct default explicitly.' -Command { (Get-CimInstance Win32_Printer -Filter "Name='Your Printer Name'") | Invoke-CimMethod -MethodName SetDefaultPrinter } Invoke-Step -Number 5 -Do 'Check no printer is stuck in the offline state, which makes Office behave as though it does not exist.' -Command { Get-Printer | Select-Object Name, PrinterStatus, @{n='Offline';e={(Get-CimInstance Win32_Printer -Filter "Name='$($_.Name)'").WorkOffline}} } Invoke-Step -Number 6 -Do 'Restart Office completely. It reads the default at startup and will not notice a change made while it was open.' -Manual Write-Rule " Confirm it worked" Show-Prose 'The default printer is correct, survives a reboot, and Office prints to it.' '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-printing' 'Yellow' } else { Write-Host ' Finished.' -ForegroundColor Green } Write-Host '' Show-Prose 'To undo: Set LegacyDefaultPrinterMode back to 0 to let Windows manage the default again.' 'DarkGray' Write-Rule