You can really get battery / ac status via:
wmic path Win32_Battery Get BatteryStatus
But evaluating the value of a state is not just a question of "is 1" versus "is 1"!
Check out:
https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-battery
For example, when an AC is connected, you should usually get "2"
Unknown (2)
The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging.
But you can also get a collection of other values.
I am pretty sure that all this means "on battery" / "not on alternating current":
Other (1) Low (4) Critical (5)
And all this means "on alternating current" / "not on battery":
Unknown (2) Charging (6) Charging and high (7) Charging and low (8) Charging and critical load (9)
I strongly suggest that this also indicates "on alternating current":
Fully charged (3)
It seems less certain ...
Not defined (10) Partially charged (11)
I would suggest that "Undefined (10)" means "on alternating current" / "without battery". And "Partially charged (11)" should mean "on battery", but whether "AC is turned on" seems rather foggy for this (last, odd) listing.
In addition, usually when there is no battery on the machine, the following message is returned instead:
"Instances are not available."
Thus, for my purposes, I have identified 4 basic βstatesβ for the battery, which should be in:
- DISCHARGE
- CHARGING
- FULL
- NOT PRESENT
Linux has direct analogues that can be requested from the kernel.
In my logic, I consider the first "No Instance(s) Available." == NOT PRESENT "No Instance(s) Available." == NOT PRESENT "No Instance(s) Available." == NOT PRESENT "No Instance(s) Available." == NOT PRESENT , then I used the following ratings for status codes:
DISCHARGING Other (1) Low (4) Critical (5) CHARGING Unknown (2) Charging (6) Charging and High (7) Charging and Low (8) Charging and Critical (9) Partially Charged (11) FULL Fully Charged (3) NOT PRESENT Undefined (10)
In addition, I wanted a logical check for "isBatteryPresent" and "isAcPower". I relied on my assessment of the condition, and then calculated the following:
isBatteryPresent = state != NOT PRESENT isAcPower = state != DISCHARGING