I’m trying to create a PowerShell Script where it will check, install and auto-reboot after Windows Updates on the local computer, this question has been asked for like a million times all across the internet, here’s what I tried:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module PSWindowsUpdate -Force
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
Import-Module PSWindowsUpdate -Force
Get-WindowsUpdate -AcceptAll -AutoReboot -Install
But using PSWindowsUpdate
is unreliable since it sometimes will error out as stated some comments (Exception from HRESULT
)
So I tried some more commands such as
Start-Process -FilePath "$env:SystemRoot\System32\UsoClient.exe" -ArgumentList StartInteractiveScan
But this one is only for checking for windows updates, it doesn’t install them and it does not auto-reboot.
I also tried
PowerShell.exe (New-Object -ComObject Microsoft.Update.AutoUpdate).DetectNow()
It doesn’t seem to do anything, and if I’m not mistaken I remember reading a comment saying the DetectNow
is no longer wokring (?)
I also tried
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -force
$thisComputer = $env:COMPUTERNAME
#Define update criteria.
$Criteria = "IsInstalled=0 and Type='Software'"
#Search for relevant updates.
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$SearchResult = $Searcher.Search($Criteria).Updates
#Download updates.
$Session = New-Object -ComObject Microsoft.Update.Session
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $SearchResult
$Downloader.Download()
#Install updates.
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $SearchResult
$Result = $Installer.Install()
#Reboot if required by updates.
If ($Result.rebootRequired)
{
shutdown.exe /t 0 /r
}
But this doesn’t seem to do anything.
I even tried
Start-Process -FilePath 'ms-settings:windowsupdate' -WindowStyle Normal
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('{TAB}')
[System.Windows.Forms.SendKeys]::SendWait('{TAB}')
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
[System.Windows.Forms.SendKeys]::SendWait('{TAB}')
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
But this approach is not reliable since sometimes when it opens ms-settings:windowsupdate
the “Check Now” button is not visible (because it’s already checking for updates, but won’t install them)
I found this article but I have no idea how implement it or if it’s answers my needs
Theoretically it sounds simple, all I need to a script that will check, install, reboot windows updates, realistically - I have not found any reliable way of doing so.
- Windows 10 IoT Enterprise LTSC 2021
- Fresh Install (on VM)
- PowerShell ISE (for testing)