Download latest version of SumatraPDF and install it for all Users with PDF preview in Explorer:
$ProgressPreference = 'SilentlyContinue'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$html = (Invoke-WebRequest -Uri 'https://www.sumatrapdfreader.org/download-free-pdf-viewer' -UseBasicParsing).Content
$ver = [regex]::Matches($html, 'SumatraPDF-(\d+(?:\.\d+)+)-64-install\.exe') |
ForEach-Object { [version]$_.Groups[1].Value } | Sort-Object -Descending | Select-Object -First 1
if (-not $ver) { 'No SumatraPDF version found'; return }
$file = "C:\Users\Public\Downloads\SumatraPDF-$ver-64-install.exe"
Invoke-WebRequest -Uri "https://www.sumatrapdfreader.org/dl/rel/$ver/SumatraPDF-$ver-64-install.exe" -OutFile $file -UseBasicParsing
Start-Process -FilePath $file -ArgumentList '-s -all-users -with-preview' -Wait
Remove-Item $file, 'C:\Users\Public\Desktop\SumatraPDF.lnk', 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\SumatraPDF.lnk' -ErrorAction SilentlyContinue
"SumatraPDF $ver installed"
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."
}
}