Download latest version of SumatraPDF and install it for all Users with PDF preview in Explorer:
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -Value 2
$ProgressPreference = "SilentlyContinue"
$url = "https://www.sumatrapdfreader.org/download-free-pdf-viewer"
$html = Invoke-WebRequest -Uri $url
$pattern = 'SumatraPDF-(\d+(\.\d+)+)-64-install\.exe'
$matches = $html.AllElements | Where-Object { $_.tagName -eq "a" -and $_.innerText -match $pattern } | ForEach-Object { $_.innerText }
$latestVersion = $matches | Sort-Object -Descending | Select-Object -First 1
$latestVersion -match 'SumatraPDF-(\d+(\.\d+)+)-64-install\.exe'
$SumatraVersion = $matches[1]
$Link = "https://www.sumatrapdfreader.org/dl/rel/$SumatraVersion/$latestVersion"
$Path = "C:\Users\Public\Downloads"
$File = "SumatraPDF-latest-64-install.exe"
Invoke-WebRequest -Uri $Link -OutFile "$Path\$File" -UseBasicParsing
Start-Process -FilePath "$Path\$File" -ArgumentList "-s -all-users -with-preview" -Wait
reg add "HKCU\Software\Microsoft\Print\UnifiedPrintDialog" /v "PreferLegacyPrintDialog" /d 1 /t REG_DWORD /f
Remove-Item "C:\Users\Public\Desktop\SumatraPDF.lnk"
Remove-Item "C:\ProgramData\Microsoft\Windows\Start Menu\SumatraPDF.lnk"
try {
# --- Get latest version from GitHub ---
Write-Host "Checking latest SumatraPDF version..."
$ProgressPreference = "SilentlyContinue"
$ApiUrl = "https://api.github.com/repos/sumatrapdfreader/sumatrapdf/releases/latest"
$Release = Invoke-RestMethod -Uri $ApiUrl -UseBasicParsing
$Version = $Release.tag_name -replace 'rel$', '' # e.g. "3.5.2rel" -> "3.5.2"
Write-Host "Latest version: $Version"
# --- Build download URL ---
$FileName = "SumatraPDF-$Version-64-install.exe"
$Url = "https://www.sumatrapdfreader.org/dl/rel/$Version/$FileName"
$TempPath = Join-Path $env:TEMP $FileName
# --- Download ---
Write-Host "Downloading from $Url ..."
Invoke-WebRequest -Uri $Url -OutFile $TempPath -UseBasicParsing
# --- Install silently ---
Write-Host "Installing silently..."
$proc = Start-Process -FilePath $TempPath -ArgumentList "-s -all-users -with-preview" -Wait -PassThru
if ($proc.ExitCode -eq 0) {
Write-Host "SumatraPDF $Version installed successfully." -ForegroundColor Green
} else {
Write-Warning "Installer exited with code $($proc.ExitCode)."
}
} catch {
Write-Error "Failed: $_"
} finally {
if ($TempPath -and (Test-Path $TempPath)) {
Remove-Item $TempPath -Force
Write-Host "Cleaned up temp file."
}
}