Created
April 29, 2025 01:04
-
-
Save greenido/1d7a28e50664d9c626260329f582643d to your computer and use it in GitHub Desktop.
A Minimal Windows Patch Management Script - Gathers essential Windows Update info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Minimal Windows Patch Management Script | |
# Gathers essential Windows Update info | |
# Output setup | |
$OutputDir = "$env:USERPROFILE\Desktop\PatchReport" | |
if (!(Test-Path $OutputDir)) { New-Item -Path $OutputDir -ItemType Directory -Force | Out-Null } | |
$outputFile = "$OutputDir\PatchReport-$(Get-Date -Format 'yyyyMMdd-HHmmss').txt" | |
# Write helper | |
function Write-OutputFile { | |
param([string]$Text) | |
Write-Host $Text | |
Add-Content -Path $outputFile -Value $Text | |
} | |
# Start report | |
"Windows Patch Report - $(Get-Date)" | Out-File $outputFile | |
# System Info | |
Write-OutputFile "`n=== System Info ===" | |
Get-ComputerInfo | Select-Object WindowsProductName, WindowsVersion, OsArchitecture | Format-List | Out-String | Write-OutputFile | |
# Windows Update Service Status | |
Write-OutputFile "`n=== Windows Update Service ===" | |
Get-Service -Name wuauserv | Select-Object DisplayName, Status | Format-List | Out-String | Write-OutputFile | |
# Windows Update Settings (Registry) | |
Write-OutputFile "`n=== Update Policy ===" | |
$wuPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" | |
if (Test-Path $wuPath) { | |
Get-ItemProperty -Path $wuPath | Format-List | Out-String | Write-OutputFile | |
} else { | |
Write-OutputFile "No custom Windows Update policy." | |
} | |
# Update History (Last 10) | |
Write-OutputFile "`n=== Last 10 Updates ===" | |
try { | |
$session = New-Object -ComObject Microsoft.Update.Session | |
$searcher = $session.CreateUpdateSearcher() | |
$history = $searcher.QueryHistory(0,10) | |
$history | Select-Object Date, Title, @{Name="Result";Expression={ | |
switch($_.ResultCode) { | |
2 {"Succeeded"} 3 {"Succeeded With Errors"} 4 {"Failed"} default {"Other"} | |
} | |
}} | Format-Table -AutoSize | Out-String | Write-OutputFile | |
} catch { | |
Write-OutputFile "Failed to retrieve update history." | |
} | |
# Pending Updates (Basic COM Method) | |
Write-OutputFile "`n=== Pending Updates ===" | |
try { | |
$pending = ($session.CreateUpdateSearcher()).Search("IsInstalled=0").Updates | |
if ($pending.Count -gt 0) { | |
$pending | ForEach-Object { Write-OutputFile "- $($_.Title)" } | |
} else { | |
Write-OutputFile "No pending updates." | |
} | |
} catch { | |
Write-OutputFile "Failed to retrieve pending updates." | |
} | |
# Report Path | |
Write-Host "`nReport saved to: $outputFile" -ForegroundColor Green | |
Write-Host "Open with: notepad '$outputFile'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment