This PowerShell script utilizes the Windows Update Agent API to scan for available updates that are not yet installed, displaying a detailed list of pending updates including their titles, KB numbers, download status, and descriptions. It provides a quick and reliable way to check the update status of a Windows system without requiring manual intervention through the Settings app.
Click to view script…
# PowerShell script to check for Windows updates and list pending updates
try {
Write-Output "Checking for updates..."
# Create a new update session
$session = New-Object -ComObject Microsoft.Update.Session
# Create an update searcher
$searcher = $session.CreateUpdateSearcher()
# Set the search criteria to find updates that are not installed and not hidden
$criteria = "IsInstalled=0 and IsHidden=0"
# Perform the search
$result = $searcher.Search($criteria)
# Check if the search was successful (ResultCode 2 means Succeeded)
if ($result.ResultCode -eq 2) {
$updates = $result.Updates
if ($updates.Count -gt 0) {
Write-Output "Found $($updates.Count) pending updates:"
$counter = 1
foreach ($update in $updates) {
# Get the KB article ID if available
$kb = if ($update.KBArticleIDs.Count -gt 0) { $update.KBArticleIDs[0] } else { "N/A" }
# Check if the update is downloaded
$status = if ($update.IsDownloaded) { "Downloaded" } else { "Not Downloaded" }
Write-Output "$counter. Title: $($update.Title) (KB$kb) - $status"
Write-Output " Description: $($update.Description)"
Write-Output ""
$counter++
}
} else {
Write-Output "No pending updates found."
}
} else {
Write-Output "Update search failed with result code $($result.ResultCode)"
}
} catch {
Write-Output "An error occurred: $($_.Exception.Message)"
}