-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-drivers-from-ms.ps1
45 lines (38 loc) · 1.35 KB
/
update-drivers-from-ms.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Create a COM object to interact with the Windows Update API
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
Write-Output "Searching for available updates..."
# Search for all available updates
try {
$SearchResult = $UpdateSearcher.Search("IsInstalled=0")
} catch {
Write-Error "Error during update search: $_"
exit
}
$UpdatesToDownload = New-Object -ComObject Microsoft.Update.UpdateColl
$foundUpdates = $false
foreach ($Update in $SearchResult.Updates) {
if ($Update.Driver) {
$UpdatesToDownload.Add($Update) | Out-Null
Write-Output "Driver Update Found: $($Update.Title)"
$foundUpdates = $true
}
}
if (-not $foundUpdates) {
Write-Output "No driver updates found."
exit
}
# Ask the user if they want to download the updates
$DownloadUpdates = Read-Host "Do you want to download these updates? (Y/N)"
if ($DownloadUpdates -eq 'Y') {
$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToDownload
try {
$DownloadResult = $Downloader.Download()
Write-Output "Download Result: $($DownloadResult.ResultCode)"
} catch {
Write-Error "Error during update download: $_"
}
} else {
Write-Output "Update download skipped by user."
}