# Windows Updates - Check, Install, Reboot

**URL:** <https://forums.powershell.org/t/windows-updates-check-install-reboot/24262>\
**Category:** PowerShell Help\
**Created:** [June 11, 2024, 8:41am UTC](https://forums.powershell.org/t/windows-updates-check-install-reboot/24262 "2024-06-11T08:41:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![afbebn](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@afbebn](https://forums.powershell.org/u/afbebn)\
**Post date:** [June 11, 2024, 8:41am UTC](https://forums.powershell.org/t/windows-updates-check-install-reboot/24262/1 "2024-06-11T08:41:54Z")

</div>

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:

```powershell
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](https://community.spiceworks.com/t/fun-with-powershell-install-updates-and-reboot-if-necessary-then-get-an-e-mail/465239/37) (`Exception from HRESULT`)

So I tried some more commands such as

```powershell
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
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

```powershell
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

```powershell
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](https://learn.microsoft.com/en-us/windows/win32/api/wuapi/nn-wuapi-iautomaticupdates?redirectedfrom=MSDN) 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)

---

<div class="post-metadata">

**Author:** ![afbebn](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@afbebn](https://forums.powershell.org/u/afbebn)\
**Post date:** [June 11, 2024, 12:15pm UTC](https://forums.powershell.org/t/windows-updates-check-install-reboot/24262/2 "2024-06-11T12:15:53Z")

</div>

I think I found a solution, from here:

> <https://stackoverflow.com/questions/15175054/powershell-install-windows-updates/73115357#73115357>

```powershell
function UpdateOS(){
    Write-Host "`nUpdating OS."

    # Open Eventlogs for Windows Update
    Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{Get-Content C:\Windows\SoftwareDistribution\ReportingEvents.log -Tail 1 -Wait}"

    #Define update criteria.
    $Criteria = "IsInstalled=0"

    #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()

    $Installer = New-Object -ComObject Microsoft.Update.Installer
    $Installer.Updates = $SearchResult

    $Result = $Installer.Install()

    If ($Result.rebootRequired) { shutdown.exe /t 0 /r }
}

UpdateOS

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 11, 2024, 12:16pm UTC](https://forums.powershell.org/t/windows-updates-check-install-reboot/24262/3 "2024-07-11T12:16:03Z")

</div>

Closed on July 11, 2024, 12:16pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
