Skip to content

Commit dfab8ec

Browse files
authored
Merge pull request #22 from Miiraak/Update-Get-BatteryStatus.ps1-and-related-docs
Updating Get-BatteryStatus.ps1 and related docs
2 parents 3bbab09 + a378231 commit dfab8ec

2 files changed

Lines changed: 371 additions & 59 deletions

File tree

Lines changed: 113 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,141 @@
11
# Get-BatteryStatus.ps1
22

33
## Overview
4-
**Get-BatteryStatus.ps1** is a PowerShell script for Windows 10/11 that displays your laptop's battery percentage and charging status. The script uses the .NET `System.Windows.Forms` library to access battery information and provides a color-coded status output for quick visual feedback.
5-
6-
## Features
7-
- **Battery Detection:** Checks if a battery is present on the system.
8-
- **Battery Percentage:** Displays the current battery level as a percentage.
9-
- **Charging Status:** Shows whether the device is plugged in or running on battery.
10-
- **Color-Coded Output:** Uses different colors for different battery levels:
11-
- **Green:** 80% and above
12-
- **Blue:** 50% to 79%
13-
- **Yellow:** 20% to 49%
14-
- **Red:** Below 20% or if no battery detected
4+
**Get-BatteryStatus.ps1** is a PowerShell script for Windows 10/11 that prints a **structured battery analysis report directly in the console**.
5+
6+
It provides a clear, technician-readable summary of battery health, real-world consumption and runtime estimates — without having to open an HTML file.
7+
All data is sourced from a single **`powercfg /batteryreport`** HTML file generated temporarily at runtime.
8+
9+
## What it shows
10+
11+
### Machine identity
12+
- Computer name (from `$env`)
13+
- Computer model (from `SYSTEM PRODUCT NAME` in the powercfg report)
14+
- CPU name (via `Win32_Processor`)
15+
16+
### Battery information
17+
- **Design capacity** *(factory/original maximum)* in **Wh**
18+
- **Current capacity** *(current maximum)* in **Wh**
19+
- **Battery health (%)** computed from Design vs Current capacity
20+
21+
### Usage statistics
22+
- Number of **analyzed discharge sessions** (parsed from the powercfg report)
23+
- **Average power consumption** in **Watts** across all sessions
24+
25+
### Battery life estimation
26+
- **Estimated runtime** with the current battery
27+
- **Estimated runtime** with a new (design capacity) battery
28+
29+
### Conclusion
30+
- **Wear level (%)**
31+
- Potential **runtime gain** from replacing the battery (in minutes)
32+
33+
## Data sources
34+
All data comes from a single source:
35+
36+
| Data | Source |
37+
|---|---|
38+
| Computer name | `$env:COMPUTERNAME` |
39+
| Computer model | `powercfg /batteryreport` HTML (`SYSTEM PRODUCT NAME`) |
40+
| Design / Current capacity | `powercfg /batteryreport` HTML (`Installed batteries` table) |
41+
| Discharge sessions (duration + energy) | `powercfg /batteryreport` HTML (`Battery usage` table) |
42+
| CPU name | `Win32_Processor` (CIM, single call — not in powercfg report) |
43+
| Battery presence | `System.Windows.Forms.SystemInformation.PowerStatus` |
44+
45+
The script generates a **temporary** HTML file in `%TEMP%`, parses the needed values, then **deletes it immediately**.
1546

1647
## Parameters
17-
This script does **not** take any parameters.
48+
This script does not take any custom parameters.
49+
You can use standard PowerShell common parameters such as:
50+
- `-Verbose` to see the path of the generated report and any internal diagnostic messages.
1851

1952
## Usage
2053
```powershell
2154
.\Get-BatteryStatus.ps1
2255
```
2356

24-
## Output Example
57+
Verbose mode (recommended for troubleshooting):
58+
```powershell
59+
.\Get-BatteryStatus.ps1 -Verbose
2560
```
26-
Battery Percentage: 95% (Online)
27-
Battery Percentage: 67% (Offline)
28-
Battery Percentage: 24% (Offline)
29-
Battery Percentage: 8% (Offline)
30-
No battery detected.
61+
62+
## Output example
63+
```text
64+
==== BATTERY ANALYSIS REPORT ====
65+
66+
Computer : HP EliteBook 840 G8
67+
CPU : Intel Core i5-1135G7
68+
69+
Battery information
70+
-------------------
71+
Design Capacity : 53 Wh
72+
Current Capacity : 37 Wh
73+
Battery Health : 70 %
74+
75+
Usage statistics
76+
----------------
77+
Analyzed sessions : 34
78+
Average consumption : 12 W
79+
80+
Battery life estimation
81+
-----------------------
82+
Estimated runtime (current battery) : 2 h 33 min
83+
Estimated runtime (new battery) : 3 h 43 min
84+
85+
Conclusion
86+
----------
87+
Battery wear detected : 24 %
88+
Replacing the battery would increase runtime by ~70 minutes.
3189
```
3290

33-
## Help & Documentation
91+
## Help & documentation
3492
```powershell
3593
Get-Help .\Get-BatteryStatus.ps1
36-
man .\Get-BatteryStatus.ps1
3794
```
3895

3996
## Troubleshooting
40-
- **No Battery Detected:** If you run the script on a desktop or a device without a battery, you'll see "No battery detected."
41-
- **Requires Windows:** The script only works on Windows 10/11 and requires PowerShell 5+.
42-
- **Color Output:** Colors are visible only in terminals that support colored output.
97+
98+
### "No battery detected"
99+
You are likely running the script on a desktop, a VM, or a device without a battery.
100+
101+
### "Failed to generate battery report"
102+
`powercfg` requires elevated privileges on some systems.
103+
Try running PowerShell **as Administrator**.
104+
105+
### Capacity shows "N/A"
106+
The `powercfg /batteryreport` HTML structure may differ on non-English Windows installations.
107+
Run with `-Verbose` to confirm the report was generated, then inspect the HTML file manually before it is deleted (add a breakpoint or comment out the `finally` block temporarily).
108+
109+
### No discharge sessions found
110+
This can happen if the machine has only been used on AC power recently, or if the battery report does not contain any discharge entries for the last 7 days.
111+
112+
## Requirements
113+
- Windows 10 / 11
114+
- PowerShell 5.1 or PowerShell 7+
115+
- `powercfg.exe` available (built-in on all Windows versions)
116+
- Administrator rights may be required for `powercfg /batteryreport`
43117

44118
## License
45-
```
119+
```text
46120
MIT License
47121
48122
Copyright (c) 2025 Miiraak
49123
50-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
124+
Permission is hereby granted, free of charge, to any person obtaining a copy
125+
of this software and associated documentation files (the "Software"), to deal
126+
in the Software without restriction, including without limitation the rights
127+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
128+
copies of the Software, and to permit persons to whom the Software is
129+
furnished to do so, subject to the following conditions:
51130
52-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
131+
The above copyright notice and this permission notice shall be included in all
132+
copies or substantial portions of the Software.
53133
54-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
134+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
135+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
136+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
137+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
138+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
139+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
140+
SOFTWARE.
55141
```

0 commit comments

Comments
 (0)