Automating installing driver updates for all devices with Powershell [Windows 10]

0

I'm having some trouble with trying to use a powershell script to update all drivers on my PC without having to go through each of them. I'm using the code from the accepted answer from How to Automatically update all devices in device manager, with the added lines by user5542121 in a comment below.

#set Window Title
$host.ui.RawUI.WindowTitle = "Driver Updater"

#search and list all missing Drivers

$Session = New-Object -ComObject Microsoft.Update.Session           
$Searcher = $Session.CreateUpdateSearcher() 

$Searcher.ServiceID = '7971f918-a847-4430-9279-4a52d1efe18d'
$Searcher.SearchScope =  1 # MachineOnly
$Searcher.ServerSelection = 3 # Third Party

$Criteria = "IsInstalled=0 and Type='Driver' and IsHidden=0"
Write-Host('Searching Driver-Updates...') -Fore Green  
$SearchResult = $Searcher.Search($Criteria)          
$Updates = $SearchResult.Updates

#Show available Drivers

$Updates | select Title, DriverModel, DriverVerDate, Driverclass, DriverManufacturer | fl

#Download the Drivers from Microsoft

$UpdatesToDownload = New-Object -Com Microsoft.Update.UpdateColl
$updates | % { $UpdatesToDownload.Add($_) | out-null }
Write-Host('Downloading Drivers...')  -Fore Green  
$UpdateSession = New-Object -Com Microsoft.Update.Session
$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToDownload
$Downloader.Download()

#Check if the Drivers are all downloaded and trigger the Installation

$UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl
$updates | % { if($_.IsDownloaded) { $UpdatesToInstall.Add($_) | out-null } }

Write-Host('Installing Drivers...')  -Fore Green  
$Installer = $UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()
if($InstallationResult.RebootRequired) 
{  
    Write-Host('Reboot required! please reboot now..') -Fore Red  
} 
else 
{ 
    Write-Host('Done..') -Fore Green
}
Write-Host('Press any key to exit ...') -Fore Yellow
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

But when I run it I get the following error for the "Searching Driver-Updates..."

Exception from HRESULT: 0x80248014
At C:\Users\Amund\Desktop\update.ps1:15 char:1
+ $SearchResult = $Searcher.Search($Criteria)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Object reference not set to an instance of an object.
At C:\Users\Amund\Desktop\update.ps1:25 char:16
+ $updates | % { $UpdatesToDownload.Add($_) | out-null }
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException

and the following error for the "Downloading Drivers..."

Exception from HRESULT: 0x80240024
At C:\Users\Amund\Desktop\update.ps1:30 char:1
+ $Downloader.Download()
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

and the following error for the "Installing Drivers..."

Exception from HRESULT: 0x80240024
At C:\Users\Amund\Desktop\update.ps1:40 char:1
+ $InstallationResult = $Installer.Install()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

I'm not familiar enough with Powershell to understand what is wrong with the code shown, so I'm asking for help with figuring out what I need to either change, add, or remove to make this work as intended.

EDIT: Adding the following lines to the beginning and end respectively removed the first errors from "Searching Driver Updates..."

$UpdateSvc = New-Object -ComObject Microsoft.Update.ServiceManager            
$UpdateSvc.AddService2("7971f918-a847-4430-9279-4a52d1efe18d",7,"") 

and before the last Write-Host call:

$UpdateSvc.RemoveService("7971f918-a847-4430-9279-4a52d1efe18d")

But I still get the same errors for the "Downloading Drivers..." and "Installing Drivers..." parts.

drivers
powershell
asked on Super User Jun 7, 2019 by Torleif Hensvold • edited Jun 7, 2019 by Torleif Hensvold

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0