Files
WSUS/reset-windows-update-agent/Cleanuplight.ps1
2025-10-31 08:55:43 +01:00

52 lines
1.4 KiB
PowerShell

$global:LogPath = "C:\Windows\Temp\Wuauserv_Cleanup.log"
$ServiceName = "wuauserv"
function Write-Log {
param([string]$Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $global:LogPath -Value "$timestamp $Message"
write-host $Message
}
function Clean-WUClientFolders {
Write-Log "Cleaning up Windows Update client folders"
$windir = $env:WINDIR
$WUDownload = Join-Path $windir "SoftwareDistribution\Download"
$WUDataStore = Join-Path $windir "SoftwareDistribution\Datastore"
if (Test-Path $WUDownload) {
try {
Remove-Item $WUDownload -Recurse -Force
Write-Log "Deleted $WUDownload"
} catch {
Write-Log ("Failed to delete $WUDownload")
}
}
if (Test-Path $WUDataStore) {
try {
Remove-Item $WUDataStore -Recurse -Force
Write-Log "Deleted $WUDataStore"
} catch {
Write-Log ("Failed to delete $WUDataStore")
}
}
}
try {
Write-Log "Stopping $ServiceName"
Stop-Service -Name $ServiceName -Force -ErrorAction Stop
Start-Sleep -Seconds 3
} catch {
Write-Log ("Failed to stop $ServiceName")
}
Clean-WUClientFolders
try {
Write-Log "Starting $ServiceName"
Start-Service -Name $ServiceName -ErrorAction Stop
Write-Log "$ServiceName started"
} catch {
Write-Log ("Failed to start $ServiceName")
}