Skip to content

How to Check/Troubleshoot Windows System Health with PowerShell

cpx July 2, 2024 1 min read MSWindows (MSFT) POWERSHELL

A handy PowerShell script to get a comprehensive overview of your system’s health, including errors, reboots, and updates.

What it does: This script generates a detailed report of your Windows system’s recent events, including system errors, unexpected shutdowns, reboot history, and Windows updates.

cat > troubleshooting.ps1 <

# Get-SystemErrorsAndReboots.ps1

# Function to write section headers
function Write-SectionHeader($title) {
    Write-Host "`n===== $title =====" -ForegroundColor Cyan
}

# Get recent system errors
Write-SectionHeader "Recent System Errors"
Get-EventLog -LogName System -EntryType Error -Newest 10 | Format-Table TimeGenerated, Source, Message -AutoSize -Wrap

# Get recent critical events
Write-SectionHeader "Recent Critical Events"
Get-EventLog -LogName System -EntryType Error -Newest 10 | Where-Object {$_.EntryType -eq "Critical"} | Format-Table TimeGenerated, Source, Message -AutoSize -Wrap

# Get recent unexpected shutdowns
Write-SectionHeader "Recent Unexpected Shutdowns"
Get-EventLog -LogName System -InstanceId 41 -Newest 5 | Format-Table TimeGenerated, Message -AutoSize -Wrap

# Get recent reboot events
Write-SectionHeader "Recent Reboot Events"
Get-WinEvent -FilterHashtable @{LogName='System'; ID=1074,6006,6005,6008} -MaxEvents 10 | Format-Table TimeCreated, Id, Message -AutoSize -Wrap

# Get last boot time
Write-SectionHeader "Last Boot Time"
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object LastBootUpTime

# Get system uptime
Write-SectionHeader "System Uptime"
$bootuptime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
$uptime = (Get-Date) - $bootuptime
Write-Host "Days: $($uptime.Days), Hours: $($uptime.Hours), Minutes: $($uptime.Minutes)"

# Get Windows Update history
Write-SectionHeader "Recent Windows Updates"
Get-HotFix | Sort-Object -Property InstalledOn -Descending | Select-Object -First 10 | Format-Table HotFixID, Description, InstalledOn -AutoSize

# Pause to keep the console window open
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

0 0 votes
Article Rating
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x