5.6 KiB
5.6 KiB
Laptop Battery Testing Scripts
PowerShell scripts to stress-test laptop battery drain under a controlled synthetic CPU load, with all power-saving features (including hibernate-on-low-battery) disabled during the test, and a companion script to restore the original settings afterward.
Files
| Script | Purpose |
|---|---|
| Test-BatteryDrain.ps1 | Disables power-saving/hibernate settings, generates ~30% CPU load, logs battery drain to CSV. |
| Restore-PowerSettings.ps1 | Reverts the power scheme and hibernate settings changed by the test script. |
Both scripts self-elevate (relaunch as Administrator) if not already running elevated, since
powercfg changes require admin rights.
Test-BatteryDrain.ps1
What it does
- Backs up the currently active power scheme GUID and hibernate on/off state to
PowerSettingsBackup.json(used later by the restore script). - Switches to the Ultimate Performance plan (duplicated if not already present) or High performance as a fallback, so Windows doesn't throttle the CPU during the test.
- Disables, on both AC and battery (DC):
- Monitor, disk, standby, and hibernate timeouts.
- Standby/hibernate idle behavior at the sleep-subgroup level.
- Low/critical battery actions (set to "Do Nothing" instead of sleep/hibernate/shutdown).
- Low/critical battery notification flyouts.
- USB selective suspend.
- PCI Express link state power management (ASPM).
- Adaptive brightness and display dimming.
- Runs
powercfg /hibernate offto fully disable hibernation (removeshiberfil.sys). - Calls
SetThreadExecutionStateto keep the system awake/display on for the life of the script, as a redundant safety net. - Starts one background job per logical CPU core, each duty-cycling busy/sleep periods to
approximate the requested overall CPU load (
-TargetLoadPercent, default 30%). - Samples battery percentage/charging status every
-SampleIntervalSecondsand appends each sample to a CSV log (Timestamp,PercentRemaining,Status,Charging,EstimatedRunTimeMinutes). - Runs for
-DurationMinutes(or indefinitely if0, until Ctrl+C), then stops the CPU load jobs, clears the stay-awake flag, and prints a summary (start/end %, elapsed time, average drain rate in %/hour).
Parameters
| Parameter | Default | Description |
|---|---|---|
-DurationMinutes |
60 |
Test length in minutes. 0 = run until Ctrl+C. |
-SampleIntervalSeconds |
30 |
How often to sample/log battery status. |
-TargetLoadPercent |
30 |
Approximate overall CPU load to generate (0-100). |
-LogPath |
BatteryDrainLog_<timestamp>.csv next to the script |
CSV log output path. |
-StateFilePath |
PowerSettingsBackup.json next to the script |
Where the pre-test settings backup is saved. |
-SkipPowerSettingsChanges |
off | Only run the CPU load + logging; don't touch power settings. |
Examples
# Default: 60 min test, ~30% CPU load, sample every 30s
.\Test-BatteryDrain.ps1
# 2 hour test, ~25% load, sample every minute
.\Test-BatteryDrain.ps1 -DurationMinutes 120 -TargetLoadPercent 25 -SampleIntervalSeconds 60
# Run until manually stopped (Ctrl+C), custom log location
.\Test-BatteryDrain.ps1 -DurationMinutes 0 -LogPath D:\logs\battery.csv
Output
- A CSV log with one row per sample interval.
- Console output with each sampled reading.
- A final summary block with total percentage-point drop and average %/hour drain rate.
Restore-PowerSettings.ps1
What it does
- Runs
powercfg -restoredefaultschemesto reset Balanced/Power saver/High performance back to Windows factory defaults, undoing the timeout/USB/PCIe/battery-action changes. - Re-activates whichever power scheme was active before the test (read from the state file).
- Deletes the duplicated "Ultimate Performance" scheme if the test script created one.
- Restores hibernation to its original on/off state (
powercfg /hibernate on|off), defaulting to "on" if no state file is found. - Deletes the state file once restoration is complete.
Parameters
| Parameter | Default | Description |
|---|---|---|
-StateFilePath |
PowerSettingsBackup.json next to the script |
Path to the state file written by Test-BatteryDrain.ps1. |
Example
.\Restore-PowerSettings.ps1
Typical Workflow
# 1. Unplug the laptop charger.
# 2. Run the drain test (e.g. 2 hours at 30% load):
.\Test-BatteryDrain.ps1 -DurationMinutes 120 -TargetLoadPercent 30
# 3. Review the generated BatteryDrainLog_*.csv for drain rate / behavior.
# 4. Restore normal power behavior:
.\Restore-PowerSettings.ps1
Notes & Caveats
- Requires Administrator rights (scripts self-elevate via UAC prompt).
Test-BatteryDrain.ps1does not auto-restore settings when it finishes or is interrupted with Ctrl+C — always runRestore-PowerSettings.ps1afterward to revert the system to its normal power-saving behavior.- If
Test-BatteryDrain.ps1is interrupted before it finishes, the CPU load jobs and stay-awake flag are cleared via itsfinallyblock, but the power scheme changes remain in effect until you run the restore script. - The synthetic CPU load is an approximation (each core independently duty-cycles busy/idle
time); actual measured CPU usage may vary slightly from
-TargetLoadPercentdepending on system background activity and CPU frequency scaling. powercfg -restoredefaultschemesresets all built-in schemes to factory defaults, including any manual customizations you made to them outside of this test — not just the ones changed byTest-BatteryDrain.ps1.