Skip to content

Instantly share code, notes, and snippets.

@TimLeitch
Last active July 15, 2022 17:16
Show Gist options
  • Select an option

  • Save TimLeitch/5ce3f5e5aaa876da016724a18e12c417 to your computer and use it in GitHub Desktop.

Select an option

Save TimLeitch/5ce3f5e5aaa876da016724a18e12c417 to your computer and use it in GitHub Desktop.
Update Drivers
#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
try {
$SearchResult = $Searcher.Search($Criteria)
$Updates = $SearchResult.Updates
}
catch {
{
Write-Host('No Driver-Updates found.') -Fore Red
}
}
#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
try {
$UpdateSession = New-Object -Com Microsoft.Update.Session
$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToDownload
$Downloader.Download()
}
catch {
Write-Host('No Driver-Updates found.') -Fore Red
}
#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
try {
$Installer = $UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()
}
catch {
Write-Host ('No Drivers installed.') -Fore Red
}
if($InstallationResult.RebootRequired) {
Write-Host('Reboot required! please reboot now..') -Fore Red
} else { Write-Host('Done..') -Fore Green }
@TimLeitch
Copy link
Author

added some try catch to clean up error codes for readability

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment