0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Check Windows Updates Status

Use the PowerShell command Get-WindowsUpdateLog or the module Get-WUHistory from PSWindowsUpdate to check Windows updates; for example, Get-WUHistory | Select-Object -First 5 lists recent updates.
📋

Examples

InputGet-WUHistory | Select-Object -First 3
OutputTitle Date Result ----- ---- ------ Security Update for Windows... 4/10/2024 Succeeded Update for Microsoft Edge 4/8/2024 Succeeded Definition Update for Windows 4/7/2024 Succeeded
InputGet-WindowsUpdateLog
OutputWindows Update log created at C:\Users\User\Desktop\WindowsUpdate.log
InputGet-WUList
OutputTitle KB Article ----- ---------- Security Update for Windows... KB5003637 Feature Update to Windows 10 KB5005565
🧠

How to Think About It

To check Windows updates using PowerShell, you first decide whether to view the update history or check for available updates. You can use built-in commands or install a module like PSWindowsUpdate. The script queries the update history or available updates and displays them in a readable format.
📐

Algorithm

1
Import the PSWindowsUpdate module if not already available.
2
Query the update history or available updates using the appropriate command.
3
Select and format the output to show relevant update details.
4
Display the results to the user.
💻

Code

powershell
Import-Module PSWindowsUpdate -ErrorAction SilentlyContinue
$updates = Get-WUHistory | Select-Object -First 5
Write-Output "Recent Windows Updates:"
$updates | Format-Table -AutoSize
Output
Recent Windows Updates: Title Date Result ----- ---- ------ Security Update for Windows... 4/10/2024 Succeeded Update for Microsoft Edge 4/8/2024 Succeeded Definition Update for Windows 4/7/2024 Succeeded Cumulative Update for Windows 4/5/2024 Succeeded Security Update for Windows... 4/3/2024 Succeeded
🔍

Dry Run

Let's trace checking the last 5 Windows updates using Get-WUHistory.

1

Import Module

PSWindowsUpdate module is loaded silently if available.

2

Get Update History

Get-WUHistory fetches all update records; we select the first 5.

3

Display Results

The script outputs the update titles, dates, and results in a table.

TitleDateResult
Security Update for Windows...4/10/2024Succeeded
Update for Microsoft Edge4/8/2024Succeeded
Definition Update for Windows4/7/2024Succeeded
Cumulative Update for Windows4/5/2024Succeeded
Security Update for Windows...4/3/2024Succeeded
💡

Why This Works

Step 1: Importing the Module

The Import-Module PSWindowsUpdate command loads the module that provides commands to interact with Windows Update.

Step 2: Fetching Update History

The Get-WUHistory command retrieves the list of installed updates with details like title, date, and result.

Step 3: Selecting and Displaying

Selecting the first 5 updates with Select-Object -First 5 limits output, and Format-Table makes it easy to read.

🔄

Alternative Approaches

Using Get-HotFix
powershell
Get-HotFix | Select-Object -First 5 | Format-Table -AutoSize
This shows installed hotfixes but less detailed than PSWindowsUpdate; it's built-in and requires no extra modules.
Using Windows Update API via COM Object
powershell
$updateSession = New-Object -ComObject Microsoft.Update.Session
$updateSearcher = $updateSession.CreateUpdateSearcher()
$historyCount = $updateSearcher.GetTotalHistoryCount()
$updateHistory = $updateSearcher.QueryHistory(0, $historyCount)
$updateHistory | Select-Object -First 5 | Format-Table Title, Date, ResultCode
This method uses Windows Update API directly, no extra modules needed, but code is more complex.

Complexity: O(n) time, O(n) space

Time Complexity

The script fetches update history which grows linearly with the number of updates, so time is O(n).

Space Complexity

Storing update history requires memory proportional to the number of updates, so space is O(n).

Which Approach is Fastest?

Using Get-HotFix is fastest but less detailed; PSWindowsUpdate offers richer info but may be slower due to module overhead.

ApproachTimeSpaceBest For
PSWindowsUpdate Get-WUHistoryO(n)O(n)Detailed update history
Get-HotFixO(n)O(n)Quick hotfix list, built-in
Windows Update API COMO(n)O(n)Direct API access, no modules
💡
Install the PSWindowsUpdate module for easier and richer Windows Update management in PowerShell.
⚠️
Beginners often forget to import the PSWindowsUpdate module before running update commands, causing errors.