Check if your computer is connected to AC power in a batch file

How to check if a computer is connected to AC power in a batch file in Windows 7, for example, on_ac_power works on Linux?

+7
source share
5 answers

Direct batch file:

 WMIC Path Win32_Battery Get BatteryStatus 

Using this and some find / errorlevel , you should be able to turn it into a condition.

+8
source

Quick google 1 pulled

Here is the impressive C source code for the tool :)

 #include <windows.h> int main() { SYSTEM_POWER_STATUS status; GetSystemPowerStatus( &status ); return status.ACLineStatus; } 

hope this helps


1 http://www.google.com/search?q=windows%20powershell%20battery%20mains%20status

Note 2 : cross-compiling on Linux since I don't have Windows. This works under wine, though, the conclusion is:

  $. / battery.exe 
 255% ->   
 Amount of time remaining is unknown

+6
source
 set OnAC=false set cmd=WMIC /NameSpace:\\root\WMI Path BatteryStatus Get PowerOnline %cmd% | find /i "true" > nul && set OnAC=true if %OnAC% == true *Do your thing here* 
+2
source

Here is the script that I use in our environment that works great:

 wmic path Win32_Battery Get BatteryStatus | find /v "BatteryStatus" | find "1" >nul 2>&1 if "%errorlevel%" == "0" (echo Do whatever you want if on BATTERY) else (echo Do whatever you want if on AC POWER) 

Description:

From the wmic command, highlight the output number.

Try to find the number "1" as a result. If so, then the computer only runs on battery power. Official terminology: "(1) Battery is running low."

In addition, the computer is connected to AC power.

+2
source

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 
0
source

Source: https://habr.com/ru/post/896931/


All Articles