List Windows Updates with Powershell

PowerShell

Overview

The Get-AvailableUpdates function is a custom cmdlet written in PowerShell that searches for and displays available updates on your system. It’s like having a personal update wizard at your beck and call.

function Get-AvailableUpdates {
    [CmdletBinding()]
    Param()

    $wu_session = New-Object -ComObject Microsoft.Update.Session
    $wu_searcher = $wu_session.CreateUpdateSearcher()
    $updates = $wu_searcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

    return $updates.Updates `
    | Select-Object @{Name = 'KB'; Expression = { $_.KbArticleIds } }, `
    @{Name = 'Size (MB)'; Expression = { [math]::truncate($_.MaxDownloadSize / 1MB) } }, `
        Title, `
        IsDownloaded, `
        IsHidden, `
        IsPresent, `
        RebootRequired, `
        IsInstalled `
    | Format-Table -AutoSize -wrap
}; Get-AvailableUpdates

What Does This Code Do?

This magic code does the following:

  1. Starts a Windows Update session: It’s like opening up a special window where we can talk to the Windows Update system.
  2. Searches for updates: The code looks around and finds all the new updates that are available but haven’t been installed yet.
  3. Processes the updates: It organizes all this information in a neat table, showing you things like:
    • KB Number: A special code for each update.
    • Size (MB): How big each update is.
    • Title: What the update does.
    • Whether it needs a reboot.
  4. Displays the results: It shows you all this information in an easy-to-read table.

Outcome

When executed, the Get-AvailableUpdates function displays a formatted table of available updates on your system. Each row represents an update, showcasing its KB article ID, download size, title, and other relevant details.

Here’s an example output:

KB Size (MB) Title IsDownloaded RebootRequired IsInstalled
12345678 100 Update 1 False True False

The Get-AvailableUpdates function is a powerful tool for managing Windows updates in PowerShell. By understanding how it works, you can create your own custom cmdlets and scripts to automate update management tasks.