> ## Documentation Index
> Fetch the complete documentation index at: https://docs.digitalfyre.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Windows Update Script (PowerShell)

A PowerShell script that installs the `PSWindowsUpdate` module and applies all available Windows updates. Supports optional automatic reboot when kernel or driver updates require it.

## Prerequisites

The script must be run in an **elevated** (Administrator) PowerShell session. Right-click PowerShell and select **Run as Administrator**, or open Windows Terminal as Administrator.

## The Script

```powershell windows-update.ps1 expandable theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
#Requires -RunAsAdministrator

<#
.SYNOPSIS
    Installs all available Windows updates.
.DESCRIPTION
    Installs the PSWindowsUpdate module if not present, then downloads and
    installs all available updates. Use -AutoReboot to automatically restart
    when required.
.PARAMETER AutoReboot
    Automatically restart the server if updates require it.
#>
param(
    [switch]$AutoReboot
)

$ErrorActionPreference = "Stop"

# Install PSWindowsUpdate module if not already present
if (-not (Get-Module -ListAvailable -Name PSWindowsUpdate)) {
    Write-Host "Installing PSWindowsUpdate module..." -ForegroundColor Cyan
    Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force | Out-Null
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
    Install-Module -Name PSWindowsUpdate -Force -Confirm:$false
}

Import-Module PSWindowsUpdate

# Check for available updates
Write-Host "`nChecking for updates..." -ForegroundColor Cyan
$updates = Get-WindowsUpdate

if ($updates.Count -eq 0) {
    Write-Host "No updates available." -ForegroundColor Green
    exit 0
}

Write-Host "Found $($updates.Count) update(s):" -ForegroundColor Yellow
$updates | Format-Table -Property Title, Size, Status -AutoSize

# Install all updates
Write-Host "Installing updates..." -ForegroundColor Cyan
if ($AutoReboot) {
    Install-WindowsUpdate -AcceptAll -AutoReboot
} else {
    Install-WindowsUpdate -AcceptAll -IgnoreReboot
}

# Check reboot status
if (Get-WURebootStatus -Silent) {
    Write-Host "`n*** A reboot is required to complete the update. ***" -ForegroundColor Yellow
    if (-not $AutoReboot) {
        Write-Host "Run 'Restart-Computer' or reboot manually when ready." -ForegroundColor Yellow
    }
} else {
    Write-Host "`nAll updates installed successfully." -ForegroundColor Green
}
```

## Usage

```powershell theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
# Save the script and run it
.\windows-update.ps1

# Run with automatic reboot
.\windows-update.ps1 -AutoReboot
```

:::caution If your PowerShell execution policy blocks scripts, you will need to allow it for the current session:

```powershell theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
```

This only affects the current session and does not change the system-wide policy. :::

## Scheduling with Task Scheduler

To run updates on a schedule:

1. Open **Task Scheduler** (`taskschd.msc`)
2. Create a new task with **Run with highest privileges** checked
3. Set the trigger to your preferred schedule (e.g., weekly on Sundays at 3:00 AM)
4. Set the action to:
   * **Program:** `powershell.exe`
   * **Arguments:** `-ExecutionPolicy Bypass -File "C:\Scripts\windows-update.ps1"`

Add `-AutoReboot` to the arguments if you want the server to reboot automatically after updates.

## What PSWindowsUpdate Provides

The `PSWindowsUpdate` Module is a widely used community module that wraps the Windows Update Agent API. Key commands:

| Command                            | Description                                |
| ---------------------------------- | ------------------------------------------ |
| `Get-WindowsUpdate`                | List available updates without installing  |
| `Install-WindowsUpdate -AcceptAll` | Download and install all available updates |
| `Get-WUHistory`                    | View update installation history           |
| `Get-WURebootStatus`               | Check if a reboot is pending               |
