Trigging Windows Update from Autounattend.xml

Posted by Trilobyte-177@reddit | sysadmin | View on Reddit | 5 comments

Hello Everyone,

I've got a Windows 11 Autounattend.xml that is working just fine in all but one area.

The following code is run after install during the first login of the inbuild administrator account.

Write-output "Install Nuget"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

Write-Output "Install PSWindowsUpdate"
Install-Module -Name PSWindowsUpdate -Force -AllowClobber

# Import the update module
Import-Module PSWindowsUpdate

# Ensure Windows Update service is running
Set-Service -Name wuauserv -StartupType Automatic
Start-Service -Name wuauserv

#Reset-WUComponents

# Check for available updates
Write-Output "Checking for Windows updates..."
$updates = Get-WindowsUpdate

# Display the available updates
if ($updates.Count -eq 0) {
    Write-Host "No updates available."
} else {
    Write-Host "The following updates are available:"
    $updates | ForEach-Object { Write-Host $_.Title }

    # Install the updates without restarting
    Write-Output "Installing updates..."
    Install-WindowsUpdate -AcceptAll -Install -IgnoreReboot | Out-File "c:\users\public\logs\$(Get-Date -Format yyyy-MM-dd)-WindowsUpdate.log" -Force

    Write-Host "Updates have been installed. A reboot is required but will not happen automatically."
}

Note: The log file is not generated.

There are other bits of this script that run just fine - however this is not going anywhere.

The Nuget package and the the PSWindowsUpdate package get installed (i've checked this running another PS window) but the actual contents don't seam to do anything.

Has anyone got any ideas of why this wouldn't work?