Skip to content

Instantly share code, notes, and snippets.

@dprado75
Last active May 26, 2026 00:38
Show Gist options
  • Select an option

  • Save dprado75/b86529545743fff8e9461d7728bf0e5a to your computer and use it in GitHub Desktop.

Select an option

Save dprado75/b86529545743fff8e9461d7728bf0e5a to your computer and use it in GitHub Desktop.
Windows Battery Replacement monitor script
# This script is meant to monitor a new laptop battery
#
# It saves the power level to a log file on disk to help you determine the real battery duration and to monitor any sudden power drops
# How to use it:
#
# 1 - First of all, ideally run the battery report before replacing your old unit.
# (on a Admin CMD windows)
# powercfg /batteryreport
#
# 2 - save that report for comparison later
# Create a new folder C:\BatteryTracker\
# Save the script below as battery_tracer.ps1
#
# 3 - open a new PowerShell window as an Administrator (not CMD)
# run the command below
# Set-ExecutionPolicy Bypass -Scope Process
# Select once
#
# 4 - do run the script every time your charging and discharging the battery so you can measure the battery duration and power capacity drop
# save the script below and execute it everytime the computer loads
#
# BEGIN SCRIPT battery_tracker.ps1 -----------------------------------------------------------------------------------
$LogFile = "C:\BatteryTracker\battery_log.csv"
$TargetPercentage = 5
$PollIntervalSeconds = 60
if (-not (Test-Path $LogFile)) {
Set-Content -Path $LogFile -Value "Timestamp,PercentRemaining,Capacity_mWh,Voltage_mV,PowerStatus"
}
Write-Output "Tracking active. Target: $TargetPercentage%. Polling: $PollIntervalSeconds seconds. Logging to CSV."
while ($true) {
$Battery = Get-CimInstance -ClassName Win32_Battery
# Query ACPI for real-time mWh capacity. Requires Administrator privileges.
$BatteryACPI = Get-CimInstance -Namespace root\wmi -ClassName BatteryStatus -ErrorAction SilentlyContinue
if ($null -eq $Battery) {
Write-Output "No battery detected."
Start-Sleep -Seconds $PollIntervalSeconds
continue
}
$Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
$EstimatedCharge = $Battery.EstimatedChargeRemaining
$Voltage = if ($null -ne $Battery.DesignVoltage) { $Battery.DesignVoltage } else { "N/A" }
# Extract mWh capacity if the WMI call succeeds
$RemainingMWH = if ($null -ne $BatteryACPI) { $BatteryACPI.RemainingCapacity } else { "N/A" }
$StatusString = switch ($Battery.BatteryStatus) {
1 { "Discharging" }
2 { "AC Power" }
3 { "Fully Charged" }
4 { "Low" }
5 { "Critical" }
6 { "Charging" }
default { "Unknown" }
}
$LogLine = "$Timestamp,$EstimatedCharge,$RemainingMWH,$Voltage,$StatusString"
Add-Content -Path $LogFile -Value $LogLine
Write-Output $LogLine
if ($EstimatedCharge -le $TargetPercentage) {
Write-Output "Target reached. Shutting down."
shutdown.exe /s /f /t 10 /c "Battery threshold $EstimatedCharge% reached."
break
}
Start-Sleep -Seconds $PollIntervalSeconds
}
# END SCRIPT battery_tracker.ps1 -----------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment