Windows — Gpupdate
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
:: 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 )
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%" ) windows gpupdate
<# .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 #> param( [string]$ComputerName = $env:COMPUTERNAME, [bool]$Force = $true, [bool]$RebootIfNeeded = $false )
Write-Host "Log saved to: $logFile" :: Force policy update & wait up to 5 minutes gpupdate /force /wait:300 :: Force & reboot if necessary (for security policies) gpupdate /force /boot set DOMAIN_MODE=1 ) if %errorLevel% equ 0 (
:: Show current applied policies (without updating) gpresult /r
@echo off title GPUpdate Smart Tool color 0A echo ============================================== echo SMART GPUPDATE UTILITY echo ============================================== echo. :: Check admin rights net session >nul 2>&1 if %errorLevel% neq 0 ( echo [ERROR] Please run as Administrator. pause exit /b 1 ) echo [%DATE% %TIME%] Failed with code %errorLevel% >>
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss" $logFile = "$env:TEMP\GPUpdate_$ComputerName`_$timestamp.log"