Windows Gpupdate __exclusive__ 【2027】

:: Perform /force echo. echo [ACTION] Running gpupdate /force /wait:120 ... gpupdate /force /wait:120 >> "%LOGFILE%" 2>&1

$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

echo. echo [DONE] Log saved to: %LOGFILE% pause exit /b 0 More flexible, supports remote computers. Save as Invoke-GPUpdate.ps1 . :: Perform /force echo

:: Show pending reboot/logoff echo. echo [INFO] Checking pending actions... gpresult /scope computer /z | findstr /i "Reboot required" > nul if %errorLevel% equ 0 ( echo [ACTION] Computer reboot is REQUIRED. set PENDING=REBOOT ) else ( gpresult /scope user /z | findstr /i "Logoff required" > nul if %errorLevel% equ 0 ( echo [ACTION] User logoff is REQUIRED. set PENDING=LOGOFF ) else ( echo [OK] No reboot or logoff required. set PENDING=NONE ) ) echo [DONE] Log saved to: %LOGFILE% pause exit

📌 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.

if ($ComputerName -eq $env:COMPUTERNAME) Write-Host "Running gpupdate locally..." -ForegroundColor Cyan $params = if ($Force) "/force" else "" $result = Start-Process -Wait -NoNewWindow -FilePath "gpupdate.exe" -ArgumentList $params -PassThru $exitCode = $result.ExitCode else Write-Host "Running gpupdate on $ComputerName..." -ForegroundColor Cyan $scriptBlock = param($forceFlag) if ($forceFlag) gpupdate /force else gpupdate