# 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](Test-BatteryDrain.ps1) | Disables power-saving/hibernate settings, generates ~30% CPU load, logs battery drain to CSV. | | [Restore-PowerSettings.ps1](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 1. Backs up the currently active power scheme GUID and hibernate on/off state to `PowerSettingsBackup.json` (used later by the restore script). 2. 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. 3. 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. 4. Runs `powercfg /hibernate off` to fully disable hibernation (removes `hiberfil.sys`). 5. Calls `SetThreadExecutionState` to keep the system awake/display on for the life of the script, as a redundant safety net. 6. Starts one background job per logical CPU core, each duty-cycling busy/sleep periods to approximate the requested overall CPU load (`-TargetLoadPercent`, default 30%). 7. Samples battery percentage/charging status every `-SampleIntervalSeconds` and appends each sample to a CSV log (`Timestamp,PercentRemaining,Status,Charging,EstimatedRunTimeMinutes`). 8. Runs for `-DurationMinutes` (or indefinitely if `0`, 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_.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 ```powershell # 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 1. Runs `powercfg -restoredefaultschemes` to reset Balanced/Power saver/High performance back to Windows factory defaults, undoing the timeout/USB/PCIe/battery-action changes. 2. Re-activates whichever power scheme was active before the test (read from the state file). 3. Deletes the duplicated "Ultimate Performance" scheme if the test script created one. 4. Restores hibernation to its original on/off state (`powercfg /hibernate on|off`), defaulting to "on" if no state file is found. 5. 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 ```powershell .\Restore-PowerSettings.ps1 ``` ## Typical Workflow ```powershell # 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.ps1` does **not** auto-restore settings when it finishes or is interrupted with Ctrl+C — always run `Restore-PowerSettings.ps1` afterward to revert the system to its normal power-saving behavior. - If `Test-BatteryDrain.ps1` is interrupted before it finishes, the CPU load jobs and stay-awake flag are cleared via its `finally` block, 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 `-TargetLoadPercent` depending on system background activity and CPU frequency scaling. - `powercfg -restoredefaultschemes` resets **all** built-in schemes to factory defaults, including any manual customizations you made to them outside of this test — not just the ones changed by `Test-BatteryDrain.ps1`.