-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInstall-MicrosoftUpdate.ps1
More file actions
138 lines (102 loc) · 4.54 KB
/
Install-MicrosoftUpdate.ps1
File metadata and controls
138 lines (102 loc) · 4.54 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Function Install-MicrosoftUpdate {
<#
.SYNOPSIS
Installs Windows updates. Supply 1 or more KB IDs or it will install all pending updates
.DESCRIPTION
This cmdlet searches for matching updates, downloads any that are not already cached and then installs them.
This works on PowerShell 2.0+ but requires elevation.
.PARAMETER KBId
One or more KB article identifiers (e.g. KB5006670).
If this parameter is supplied the function looks up only those updates;
otherwise it falls back to the broader search controlled by the other switches.
.PARAMETER DownloadOnly
This parameter tells the cmdlet to download/stage the updates but does NOT install them.
.PARAMETER IncludeHidden
Include hidden updates in the search (only relevant when -KBId is not supplied).
.PARAMETER ShowInstalled
Return installed updates instead of pending ones (again, only when -KBId is not supplied).
.EXAMPLE
PS> Install-MicrosoftUpdate -KBId KB5006670,KB5008601
# This example installs the two specified updates.
.EXAMPLE
PS> Install-MicrosoftUpdate -IncludeHidden
# This example installs all pending updates, including hidden updates.
.NOTES
Last Updated: 9/1/2025
Author: Robert H. Osborne
Contact: [email protected]
.LINK
https://osbornepro.com
#>
[OutputType([PSCustomObject])]
[CmdletBinding()]
param (
[Parameter(
Position = 0,
ValueFromRemainingArguments = $True
)] # End Parameter
[String[]]$KBId,
[Switch]$DownloadOnly,
[Switch]$IncludeHidden,
[Switch]$IncludeDriver
) # End param
$InfoPref = $InformationPreference
$InformationPreference = 'Continue'
Try {
$WuService = Get-Service -Name wuauserv -ErrorAction Stop
If ($WuService.Status -ne 'Running') {
Start-Service -Name wuauserv -Confirm:$False -ErrorAction Stop
} # End If
If ($KBId) {
$KbClauses = $KBId | ForEach-Object -Process {
"KBArticleIDs='$($_)'"
} # End ForEach-Object
$BaseQuery = '(' + ($KbClauses -join ' OR ') + ')'
} Else {
$BaseQuery = "IsInstalled=0 and Type='Software'"
$BaseQuery += If ($IncludeHidden.IsPresent) { " and IsHidden=1" }
} # End If Else
Write-Debug -Message "Windows Update query: $BaseQuery"
$Searcher = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher()
$SearchResult = $Searcher.Search($BaseQuery)
If ($SearchResult.Updates.Count -eq 0) {
Write-Information -MessageData "No matching updates were found."
Return
} # End If
$UpdatesToInstall = New-Object -ComObject Microsoft.Update.UpdateColl
$SearchResult.Updates | ForEach-Object -Process {
$UpdatesToInstall.Add($_) | Out-Null
} # End ForEach-Object
$Downloader = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToInstall
Write-Information -MessageData "Downloading $($UpdatesToInstall.Count) update(s)"
$DownloadResult = $Downloader.Download()
If ($DownloadResult.ResultCode -ne 2) { # 2 = Succeeded
Throw "Download failed (ResultCode=$($DownloadResult.ResultCode))."
} # End If
$RebootRequired = "No"
If (-not $DownloadOnly.IsPresent) {
$Installer = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
Write-Information -MessageData "Installing $($UpdatesToInstall.Count) update(s)"
$InstallResult = $Installer.Install()
If ($InstallResult.ResultCode -ne 2) {
Throw "Installation failed (ResultCode=$($InstallResult.ResultCode))."
} # End If
$RebootRequired = $InstallResult.RebootRequired
} # End If
$Summary = [PSCustomObject]@{
TotalUpdates = $UpdatesToInstall.Count
InstalledKBs = ($UpdatesToInstall | ForEach-Object -Process { $_.KBArticleIDs -join ', ' })
RebootRequired = $RebootRequired
Timestamp = Get-Date -Format 'MM-dd-yyyy hh:mm:ss'
} # End PSCustomObject
Return $Summary
} Catch {
$HR = $_.Exception.HResult
$Msg = $_.Exception.Message
Throw "Failed to install updates (HRESULT: 0x{0:X8}) – {1}" -f $HR, $Msg
} Finally {
$InformationPreference = $InfoPref
} # End Try Catch Finally
} # End Function Install-MicrosoftUpdate