PowerShell Script to Check Windows Updates Status
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
How to Think About It
Algorithm
Code
Import-Module PSWindowsUpdate -ErrorAction SilentlyContinue $updates = Get-WUHistory | Select-Object -First 5 Write-Output "Recent Windows Updates:" $updates | Format-Table -AutoSize
Dry Run
Let's trace checking the last 5 Windows updates using Get-WUHistory.
Import Module
PSWindowsUpdate module is loaded silently if available.
Get Update History
Get-WUHistory fetches all update records; we select the first 5.
Display Results
The script outputs the update titles, dates, and results in a table.
| 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 |
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
Get-HotFix | Select-Object -First 5 | Format-Table -AutoSize$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
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| PSWindowsUpdate Get-WUHistory | O(n) | O(n) | Detailed update history |
| Get-HotFix | O(n) | O(n) | Quick hotfix list, built-in |
| Windows Update API COM | O(n) | O(n) | Direct API access, no modules |