:: Log file set LOGFILE=%USERPROFILE%\Desktop\GPUpdate_Log_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%.txt set LOGFILE=%LOGFILE: =0%
if %errorLevel% equ 0 ( echo [SUCCESS] Policy update completed. echo [%DATE% %TIME%] Success >> "%LOGFILE%" ) else ( echo [FAILED] Error code %errorLevel%. Check log. echo [%DATE% %TIME%] Failed with code %errorLevel% >> "%LOGFILE%" )
$result = Invoke-Command -ComputerName $ComputerName -ScriptBlock $scriptBlock -ArgumentList $Force $exitCode = $result.ExitCode "[$(Get-Date)] Exit code: $exitCode" | Out-File -FilePath $logFile -Append Interpret exit codes switch ($exitCode) 0 Write-Host "Success: Policy updated." -ForegroundColor Green 1 Write-Host "Warning: Policy updated but reboot required." -ForegroundColor Yellow 2 Write-Host "Warning: Policy updated but logoff required." -ForegroundColor Yellow 3 Write-Host "Error: Access denied (run as admin)." -ForegroundColor Red default Write-Host "Unknown error code: $exitCode" -ForegroundColor Red Auto-reboot if flagged if ($exitCode -eq 1 -and $RebootIfNeeded -and $ComputerName -eq $env:COMPUTERNAME) Write-Host "Rebooting in 30 seconds..." -ForegroundColor Magenta shutdown /r /t 30 /c "GPUpdate required reboot" windows gpupdate
:: Offer to reboot/logoff if "%PENDING%"=="REBOOT" ( echo. choice /c YN /m "Reboot now? " if errorlevel 1 shutdown /r /t 30 /c "GPUpdate requires reboot" exit /b 0 ) if "%PENDING%"=="LOGOFF" ( echo. choice /c YN /m "Logoff now? " if errorlevel 1 shutdown /l exit /b 0 )
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss" $logFile = "$env:TEMP\GPUpdate_$ComputerName`_$timestamp.log" echo [%DATE% %TIME%] Failed with code %errorLevel% >>
📌 Quick Reference Card (for daily use) | Task | Command | |------|---------| | Refresh both User & Computer policies (foreground) | gpupdate /force | | Refresh only Computer policy | gpupdate /target:computer | | Refresh only User policy | gpupdate /target:user | | Refresh & reboot if needed | gpupdate /boot | | Refresh & logoff if needed | gpupdate /logoff | | Refresh synchronously (apply sequentially) | gpupdate /sync | | Wait indefinitely for policy to finish | gpupdate /wait:0 | | See what changed (no action) | gpupdate /? | 🚀 Advanced Batch Script: SmartGPUpdate.cmd Save this as SmartGPUpdate.cmd and run as Administrator — it auto-detects domain membership, logs results, and offers reboot/logoff.
:: Check domain membership echo [INFO] Checking domain membership... gpresult /scope computer /z > nul 2>&1 if %errorLevel% neq 0 ( echo [WARN] Not joined to a domain — running local policy refresh only. set DOMAIN_MODE=0 ) else ( echo [OK] Domain policy detected. set DOMAIN_MODE=1 ) choice /c YN /m "Logoff now
:: Force & logoff if necessary (for user policies) gpupdate /force /logoff