windowspowershellupdateswindows-update

Windows 10 or 11 Feature update to 22H2 using powershell


I would like to update all my windows 10 and windows 11 machines with a Powershell script. I have a RMM agent installed on those computers and i want to invoke the OS to install the 22H2 Feature update. (My RMM Agent doesn't have a proper Patch/Windows update management, thats why i'm will to try it via a Powershell script) For some reason i'm hitting against the wall and receive the following error:

Index was outside the bounds of the array.
At line:42 char:5
+     $downloader.Updates = $featureUpdate
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException
 
Exception from HRESULT: 0x80240004

My code:

# Check if running on Windows 10 or Windows 11
$osVersion = (Get-WmiObject -Class Win32_OperatingSystem).Version
if (-not ($osVersion -match "^10\." -or $osVersion -match "^11\.")) {
    Write-Host "No Windows 10 or Windows 11 detected! Exiting..."
    exit
}

# Check if Windows Update service is running
$wuaService = Get-Service -Name wuauserv
if ($wuaService.Status -ne "Running") {
    Write-Host "Windows Update service is not running. Please start the service and try again."
    exit
}

# Check if System Restore is enabled
if ((Get-ComputerRestorePoint).Count -eq 0) {
    Write-Host "System Restore is not enabled on this system. Please enable it and try again."
    exit
}

# Create a system restore point
$timestamp = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
$restorePointDescription = "Restorepoint - $timestamp"
Checkpoint-Computer -Description $restorePointDescription -RestorePointType "MODIFY_SETTINGS"

# Create a Windows Update session
$wuaSession = New-Object -ComObject Microsoft.Update.Session

# Search for available updates
$searcher = $wuaSession.CreateUpdateSearcher()
$updates = $searcher.Search("IsInstalled=0 and DeploymentAction=*")

# Filter updates to find the feature update
$featureUpdate = $updates.Updates | Where-Object {
    $_.Title -like ("*Feature Update*")
}

# Check if a feature update is available
if ($featureUpdate) {
    # Download and install the feature update
    $downloader = $wuaSession.CreateUpdateDownloader()
    $downloader.Updates = $featureUpdate
    $downloader.Download()

    $installer = $wuaSession.CreateUpdateInstaller()
    $installer.Updates = $featureUpdate
    $installResult = $installer.Install()

    # Check the installation result
    if ($installResult.ResultCode -eq "2") {
        Write-Host "Feature update installed successfully. Please restart"
        Start-Sleep -Second 10
        exit
    } else {
        Write-Host "Feature update installation failed with error code $($installResult.ResultCode)."
    }
} else {
    Write-Host "No feature update is available."
}

The proper "Feature update to Windows 10, version 22H2" update object is found and returned, but i still get a error. I don't know what i'm doing wrong here.

Thanks in advance for your help!

The proper "Feature update to Windows 10, version 22H2" update object is found and returned, but i still get a error. When review the variable $featureUpdate i can see the correct object.


Solution

  • You need to provide the update(s) as a Microsoft.Update.UpdateColl object.

    $updatelist = New-Object -ComObject Microsoft.Update.UpdateColl
    
    # There is only one in this instance, but we'll go ahead and code for more
    foreach($update in $featureupdate){
        $null = $updatelist.Add($update)
    }
    

    Now you can add your updates to the downloader list

    $downloader.updates = $updatelist