← Back to Projects
PowerShellWindowsUtilityHardware
Battery Health Checker
A PowerShell script for Windows that checks laptop battery health and generates a detailed HTML report in the folder it's run from.
A lightweight PowerShell script that reads your Windows laptop battery stats via WMI, generates a full powercfg HTML report in the same directory, and prints a colour-coded health summary to the terminal.
Features
- Shows current charge level and charging status
- Compares full charge capacity vs original design capacity to calculate health %
- Colour-coded health rating: Good / Fair / Poor
- Generates
battery-report.html(full Windows battery report) in the script’s folder - No dependencies — uses built-in Windows tools only
Script
# battery-check.ps1
# Checks battery health and generates a detailed HTML report in the current directory.
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$ReportPath = Join-Path $ScriptDir "battery-report.html"
# Generate the full HTML report
Write-Host ""
Write-Host "Generating battery report..." -ForegroundColor Cyan
cmd /c "powercfg /batteryreport /output `"$ReportPath`"" 2>$null
if (Test-Path $ReportPath) {
Write-Host "Report saved to: $ReportPath" -ForegroundColor Green
} else {
Write-Host "Warning: Could not generate HTML report (may need to run as Administrator)." -ForegroundColor Yellow
}
# Gather battery stats
$battery = Get-WmiObject -Class Win32_Battery -ErrorAction SilentlyContinue
$fullCap = (Get-WmiObject -Namespace root/WMI -Class BatteryFullChargedCapacity -ErrorAction SilentlyContinue).FullChargedCapacity
$designCap = (Get-WmiObject -Namespace root/WMI -Class BatteryStaticData -ErrorAction SilentlyContinue).DesignedCapacity
$statusMap = @{
1 = "Discharging"
2 = "Plugged in (AC Power)"
3 = "Fully Charged"
4 = "Low"
5 = "Critical"
6 = "Charging"
7 = "Charging (High)"
8 = "Charging (Low)"
9 = "Charging (Critical)"
}
$charge = if ($battery) { "$($battery.EstimatedChargeRemaining)%" } else { "N/A" }
$statusRaw = if ($battery) { $statusMap[[int]$battery.BatteryStatus] } else { $null }
$status = if ($statusRaw) { $statusRaw } elseif ($battery) { "Unknown" } else { "N/A" }
$health = if ($fullCap -and $designCap -and $designCap -gt 0) {
[math]::Round(($fullCap / $designCap) * 100, 1)
} else { $null }
$healthLabel = if ($health -ge 80) { "Good" }
elseif ($health -ge 60) { "Fair (degraded)" }
elseif ($health) { "Poor (consider replacement)" }
else { "N/A" }
$healthColor = if ($health -ge 80) { "Green" }
elseif ($health -ge 60) { "Yellow" }
else { "Red" }
# Print summary
Write-Host ""
Write-Host "=============================" -ForegroundColor Cyan
Write-Host " BATTERY HEALTH SUMMARY " -ForegroundColor Cyan
Write-Host "=============================" -ForegroundColor Cyan
Write-Host ""
Write-Host " Current Charge : $charge"
Write-Host " Status : $status"
if ($fullCap) { Write-Host " Full Charge Cap : $([math]::Round($fullCap / 1000, 1)) Wh ($fullCap mWh)" }
if ($designCap) { Write-Host " Design Capacity : $([math]::Round($designCap / 1000, 1)) Wh ($designCap mWh)" }
if ($health) {
Write-Host " Battery Health : $health% - " -NoNewline
Write-Host $healthLabel -ForegroundColor $healthColor
}
Write-Host ""
if (Test-Path $ReportPath) {
Write-Host " Full report : $ReportPath" -ForegroundColor DarkGray
}
Write-Host "=============================" -ForegroundColor Cyan
Write-Host ""
How to run
- Save the script as
battery-check.ps1 - Open PowerShell in the same folder
- Run:
.\battery-check.ps1
Run as Administrator for the HTML report to generate correctly.
To allow scripts to run (one-time):
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
Health ratings
| Health % | Rating |
|---|---|
| 80%+ | Good |
| 60-79% | Fair (degraded) |
| Below 60% | Poor - consider replacement |