-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFind-MicrosoftUpdate.ps1
More file actions
110 lines (80 loc) · 3.22 KB
/
Find-MicrosoftUpdate.ps1
File metadata and controls
110 lines (80 loc) · 3.22 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
Function Find-MicrosoftUpdate {
<#
.SYNOPSIS
Finds available Microsoft Windows updates.
.DESCRIPTION
Queries the Windows Update COM API and returns a collection of update objects.
By default only non‑hidden, non‑driver, not‑installed updates are returned.
Use the switches to change the behaviour.
.PARAMETER IncludeHidden
Include hidden updates in the search.
.PARAMETER ShowInstalled
Return installed updates instead of pending ones.
.PARAMETER IncludeDriver
Include driver‑type updates (Category = "Drivers").
.EXAMPLE
Find-MicrosoftUpdate
# Lists pending non‑driver updates.
.EXAMPLE
Find-MicrosoftUpdate -IncludeDriver
# Lists pending updates **including** drivers.
.EXAMPLE
Find-Microsoftupdate -ShowInstalled -IncludeDriver
# Lists installed driver updates.
.NOTES
Last Updated: 9/1/2025
Author: Robert H. Osborne
Contact: rosborne@osbornepro.com
.LINK
https://osbornepro.com
#>
[OutputType([PSCustomObject])]
[CmdletBinding()]
param (
[Switch]$IncludeHidden,
[Switch]$ShowInstalled,
[Switch]$IncludeDriver
) # End param
$InfoPref = $InformationPreference
$InformationPreference = "Continue"
Try {
$RequiredService = Get-Service -Name wuauserv -ErrorAction Stop
If ($RequiredService.Status -ne "Running") {
Start-Service -Name wuauserv -Confirm:$False -ErrorAction Stop
} # End If
If ($ShowInstalled.IsPresent) {
$BaseQuery = "IsInstalled=1 and Type='Software'"
} Else {
$BaseQuery = "IsInstalled=0 and Type='Software'"
$BaseQuery += If ($IncludeHidden.IsPresent) { " and IsHidden=1" }
If (!($IncludeDriver.IsPresent)) {
$BaseQuery = "$BaseQuery and CategoryIDs not contains 2" # Exclude driver categories – the CategoryID for drivers is 2
} # End If
} # End If Else
Try {
Write-Debug -Message "Search query string: $($BaseQuery)"
$Searcher = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher()
$Result = $Searcher.Search($BaseQuery)
If ($Result.Updates.Count -eq 0) {
Write-Information -MessageData "No matching updates were found."
Return @()
} # End If
$Result.Updates | ForEach-Object -Process {
[PSCustomObject]@{
Title = $_.Title
KBIds = ($_?.KBArticleIDs -join ', ')
Description = $_.Description
IsInstalled = $_.IsInstalled
IsHidden = $_.IsHidden
Categories = ($_?.Categories | ForEach-Object -Process { $_.Name }) -join ', '
ReleaseDate = $_.LastDeploymentChangeTime
RawUpdateObject = $_ # for power‑users who need the full COM object
} # End PSCustomObject
} # End ForEach-Object
} Catch {
Throw "Failed to query Windows Update: $($Error[0].Exception.Message)"
} # End Try Catch
} Finally {
$InformationPreference = $InfoPref
} # End Try Finally
} # End Function Find-MicrosoftUpdate