Windows Gpupdate
When only machine-level settings (like security policies) are changed. /logoff
$result = Invoke-Command -ComputerName $ComputerName -ScriptBlock $scriptBlock -ArgumentList $Force $exitCode = $result.ExitCode windows gpupdate
More flexible, supports remote computers. Save as Invoke-GPUpdate.ps1 . if %errorLevel% equ 0 ( echo [SUCCESS] Policy
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%" ) echo [%DATE% %TIME%] Failed with code %errorLevel% >>
<# .SYNOPSIS Smart gpupdate with logging, reboot control, and remote support. .PARAMETER ComputerName Target computer (default: localhost). Requires PSRemoting. .PARAMETER Force Use /force (default: true) .PARAMETER RebootIfNeeded Automatically reboot if required. .EXAMPLE .\Invoke-GPUpdate.ps1 .\Invoke-GPUpdate.ps1 -ComputerName PC-001 -Force $true -RebootIfNeeded $false #>
:: 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 ) )