param([switch]$WSUS) LogMsg "Fix-WindowsUpdate.ps1" ## Delete any existing folder from a previous execution of this script If ( Test-Path "${Env:WinDir}\SoftwareDistribution.old" ) { LogMsg "Deleting ""${Env:WinDir}\SoftwareDistribution.old""" Try { Remove-Item "${Env:WinDir}\SoftwareDistribution.old" -Recurse -Force } Catch { LogErr $_.Exception.Message } } ## Stop Background Intelligent Transfer Services and Windows Update LogMsg "Stopping BITS service" Try { Stop-Service BITS } Catch { LogErr $_.Exception.Message } LogMsg "Stopping wuauserv service" Try { Stop-Service wuauserv } Catch { LogErr $_.Exception.Message } ## Rename the SoftwareDistribution folder, forcing Windows Update to recreate all metrics and redownload all updates LogMsg "Renaming ""${Env:WinDir}\SoftwareDistribution"" to ""${Env:WinDir}\SoftwareDistribution.old""" Try { Rename-Item -Path "${Env:WinDir}\SoftwareDistribution" -NewName "${Env:WinDir}\SoftwareDistribution.old" -Force } Catch { LogErr $_.Exception.Message } ## Start Windows Update and Background Intelligent Transfer Services LogMsg "Starting wuauserv service" Try { Start-Service wuauserv } Catch { LogErr $_.Exception.Message } LogMsg "Starting BITS service" Try { Start-Service BITS } Catch { LogErr $_.Exception.Message } ## Delete SoftwareDistribution.old from this execution of this script If ( Test-Path "${Env:WinDir}\SoftwareDistribution.old" ) { LogMsg "Deleting ""${Env:WinDir}\SoftwareDistribution.old""" Try { Remove-Item "${Env:WinDir}\SoftwareDistribution.old" -Recurse -Force } Catch { LogErr $_.Exception.Message } } ## If using WSUS, reset authorization with server and detect new updates If ( $WSUS ) { LogMsg "Resetting WSUS authorization and starting update detection" Try { Start-Process "${Env:WinDir}\System32\wuauctl.exe" -ArgumentList "/resetauthorization /detectnow" } Catch { LogErr $_.Exception.Message } }