Skip to content

Instantly share code, notes, and snippets.

@cicorias
Created September 17, 2025 12:19
Show Gist options
  • Select an option

  • Save cicorias/9094452537daf1759857e4a97f7455a1 to your computer and use it in GitHub Desktop.

Select an option

Save cicorias/9094452537daf1759857e4a97f7455a1 to your computer and use it in GitHub Desktop.

Revisions

  1. cicorias created this gist Sep 17, 2025.
    110 changes: 110 additions & 0 deletions resetWUS.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    <#
    Reset-WindowsUpdate.ps1 (fixed interpolation)
    Run from an elevated PowerShell window.
    #>

    # --- Guard: must be admin ---
    if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()
    ).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
    Write-Error "Run this script from an elevated PowerShell (Run as administrator)."
    exit 1
    }

    $ErrorActionPreference = 'Stop'

    Write-Host "Creating a system restore point (can take a minute)..." -ForegroundColor Cyan
    try { Checkpoint-Computer -Description "Pre Windows Update reset" -RestorePointType "MODIFY_SETTINGS" } catch { }

    # --- Services we’ll stop/start ---
    $services = @("wuauserv","bits","cryptsvc","appidsvc","msiserver")

    function Stop-Services {
    Write-Host "Stopping services..." -ForegroundColor Cyan
    foreach ($svc in $services) {
    try {
    if (Get-Service $svc -ErrorAction SilentlyContinue) {
    Stop-Service -Name $svc -Force -ErrorAction SilentlyContinue
    sc.exe config $svc start= demand | Out-Null
    Write-Host " Stopped ${svc}"
    }
    } catch {
    Write-Warning " Could not stop ${svc}: $($_.Exception.Message)"
    }
    }
    }

    function Start-Services {
    Write-Host "Starting services..." -ForegroundColor Cyan
    foreach ($svc in $services) {
    try {
    if (Get-Service $svc -ErrorAction SilentlyContinue) {
    sc.exe config $svc start= demand | Out-Null
    Start-Service -Name $svc -ErrorAction SilentlyContinue
    Write-Host " Started ${svc}"
    }
    } catch {
    Write-Warning " Could not start ${svc}: $($_.Exception.Message)"
    }
    }
    }

    Stop-Services

    # --- Clear BITS transfer queue files (qmgr*.dat) ---
    Write-Host "Clearing BITS transfer queue..." -ForegroundColor Cyan
    $bitsPath1 = Join-Path $env:ALLUSERSPROFILE "Microsoft\Network\Downloader"
    $bitsPath2 = "C:\ProgramData\Microsoft\Network\Downloader"
    Get-ChildItem -Path $bitsPath1,$bitsPath2 -Filter "qmgr*.dat" -ErrorAction SilentlyContinue | ForEach-Object {
    try { Remove-Item $_.FullName -Force -ErrorAction Continue; Write-Host " Deleted $($_.Name)" } catch {}
    }

    # --- Rename SoftwareDistribution and Catroot2 (Windows rebuilds them) ---
    $sd = Join-Path $env:SystemRoot "SoftwareDistribution"
    $sdOld = "$sd.old_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
    $cr = Join-Path $env:SystemRoot "System32\catroot2"
    $crOld = "$cr.old_$(Get-Date -Format 'yyyyMMdd_HHmmss')"

    Write-Host "Resetting SoftwareDistribution and Catroot2..." -ForegroundColor Cyan
    try {
    if (Test-Path $sd) { Rename-Item -Path $sd -NewName $sdOld -ErrorAction Stop; Write-Host " Renamed SoftwareDistribution -> $sdOld" }
    } catch { Write-Warning " Could not rename SoftwareDistribution: $($_.Exception.Message)" }

    try {
    if (Test-Path $cr) { Rename-Item -Path $cr -NewName $crOld -ErrorAction Stop; Write-Host " Renamed catroot2 -> $crOld" }
    } catch { Write-Warning " Could not rename catroot2: $($_.Exception.Message)" }

    # --- Re-register core Windows Update DLLs ---
    Write-Host "Re-registering Windows Update components..." -ForegroundColor Cyan
    $dlls = @(
    "atl.dll",
    "urlmon.dll","mshtml.dll","shdocvw.dll","browseui.dll","jscript.dll","vbscript.dll",
    "scrrun.dll","msxml.dll","msxml3.dll","msxml6.dll",
    "wintrust.dll","cryptdlg.dll","cryptui.dll","rsaenh.dll","wuapi.dll","wuaueng.dll",
    "wuaueng1.dll","wucltux.dll","wudriver.dll","wups.dll","wups2.dll","wuwebv.dll",
    "qmgr.dll","qmgrprxy.dll"
    ) | Sort-Object -Unique

    foreach ($dll in $dlls) {
    $full = Join-Path $env:SystemRoot "System32\$dll"
    if (Test-Path $full) {
    try {
    Start-Process -FilePath "$env:SystemRoot\System32\regsvr32.exe" -ArgumentList "/s `"$full`"" -Wait -WindowStyle Hidden
    Write-Host " Registered ${dll}"
    } catch {
    Write-Warning " Failed to register ${dll}: $($_.Exception.Message)"
    }
    }
    }

    # --- Reset network stacks relevant to Windows Update ---
    Write-Host "Resetting WinHTTP proxy and Winsock..." -ForegroundColor Cyan
    try { netsh winhttp reset proxy | Out-Null } catch {}
    try { netsh winsock reset | Out-Null } catch {}

    # --- Restart services ---
    Start-Services

    Write-Host "`nOptional: you can now run component repairs (commented out by default):" -ForegroundColor Yellow
    Write-Host " # sfc /scannow"
    Write-Host " # DISM /Online /Cleanup-Image /RestoreHealth"
    Write-Host "`nAll done. Reboot recommended." -ForegroundColor Green