You can update Windows locally using a PowerShell script. Here’s a script that automates the update process, including checking for updates, installing them, and restarting the system if necessary.
PowerShell Script for Local Windows Update
powershell# Run as administrator
$ErrorActionPreference = "Stop"
# Check if running as Administrator
function Test-Admin {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
if (-Not (Test-Admin)) {
Write-Host "This script must be run as Administrator!" -ForegroundColor Red
exit 1
}
# Install Windows Update module if not installed
if (-not (Get-Module -Name PSWindowsUpdate -ListAvailable)) {
Write-Host "Installing Windows Update module..."
Install-Module PSWindowsUpdate -Force -AllowClobber -Scope CurrentUser
}
# Import Windows Update module
Import-Module PSWindowsUpdate
# Check for updates
Write-Host "Checking for Windows Updates..."
$Updates = Get-WindowsUpdate -MicrosoftUpdate -AcceptAll
# Install updates
if ($Updates.Count -gt 0) {
Write-Host "Installing updates..."
Get-WindowsUpdate -Install -AcceptAll -IgnoreReboot
} else {
Write-Host "No updates available."
}
# Prompt for restart if required
if (Get-WURebootStatus) {
Write-Host "A restart is required. Restarting in 10 seconds..."
Start-Sleep -Seconds 10
Restart-Computer -Force
} else {
Write-Host "No restart required."
}
Write-Host "Windows update process completed!" -ForegroundColor Green
How to Use:
- Run PowerShell as Administrator
- Save the script as
update_windows.ps1
- Execute the script:
- powershell
Set-ExecutionPolicy Bypass -Scope Process -Force .\update_windows.ps1
This script:
- Ensures it’s running as an administrator
- Installs the
PSWindowsUpdate
module if not available - Checks for Windows updates
- Installs updates without requiring user input
- Restarts the system if necessary