8

How can I check if computer is plugged into AC power in a batch file in windows 7, like on_ac_power does in linux?

Jakob Weisblat
  • 6,663
  • 8
  • 31
  • 61

5 Answers5

9

There's a direct batch file way:

WMIC Path Win32_Battery Get BatteryStatus

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

Jon Bright
  • 12,680
  • 3
  • 28
  • 45
6

A quick google1 dragged up

Here is the - impressive - C source code to the tool :)

#include <windows.h>

int main() 
{
    SYSTEM_POWER_STATUS status;
    GetSystemPowerStatus( &status );
    return status.ACLineStatus;
}

Hope that helps


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

2 note: cross compiled on Linux since I don't have Windows. It worked under wine though, output:

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

Andriy M
  • 71,352
  • 17
  • 87
  • 142
sehe
  • 328,274
  • 43
  • 416
  • 565
  • I ended up just converting the batch file to a C program. – Jakob Weisblat Sep 09 '11 at 23:35
  • 1
    @Jake223: (if I may) study subject of the month: [powershell](http://technet.microsoft.com/en-us/scriptcenter/dd742419). It should really make a lot more sense. Yes batch files suck. But the last time I saw someone replace a setup script with a C (C++ actually) program was on AIX 5.1. _And we shook heads in silence_ – sehe Sep 09 '11 at 23:44
3

Here is the script I am using in our environnement, works nicely:

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, isolate the number from the output.

Try to find the number "1" in the result. If succesfull, it means the computer is running on battery only. The official terminology is "(1) The battery is discharging."

Else, the computer is plugged in AC power.

Rakha
  • 1,361
  • 1
  • 14
  • 42
2
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*
FearlessCoward
  • 159
  • 1
  • 5
  • "PowerOnline" does not work for me. It is also not listed in my WMIC BatteryStatus help, nor on the documentation: https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-battery What version of windows is this for? Or maybe a different battery driver perhaps? – BuvinJ Jun 03 '19 at 23:28
  • It was windows 7. I can't check battery driver anymore. – FearlessCoward Jul 18 '19 at 09:46
0

You can indeed get the battery / ac status via:

wmic path Win32_Battery Get BatteryStatus

But, evaluating the status value is not just a matter of "is 1" vs "is not 1"!

Check out:

https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-battery

For example, when the AC is plugged in, you should normally 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 could get a collection of other values as well.

I'm fairly confident these all mean "on battery" / "not on AC":

Other (1) Low (4) Critical (5)

And these all mean "on AC" / "not on battery":

Unknown (2) Charging (6) Charging and High (7) Charging and Low (8) Charging and Critical (9)

I strongly guess that this also indicates "on AC":

Fully Charged (3)

These seem less certain...

Undefined (10) Partially Charged (11)

I would guess "Undefined (10)" means "on AC" / "no battery". And "Partially Charged (11)" must mean "on battery", but whether or not the "AC is on" seems pretty nebulous for this (last, odd) enumeration.

Not also, that normally when there is no battery on the machine, this message is returned instead:

"No Instance(s) Available."

In summary, for my purposes, I defined there to be 4 main "states" for a battery to be in:

  • DISCHARGING
  • CHARGING
  • FULL
  • NOT PRESENT

On Linux, there are direct analogs for these to be queried from the kernel.

In my logic, I deem first "No Instance(s) Available." == NOT PRESENT, then I used the following evaluations for the 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)

Additionally, I wanted a boolean check for "isBatteryPresent" and "isAcPower". I leaned on my state evaluation and then deemed the following:

isBatteryPresent = state != NOT PRESENT 
isAcPower = state != DISCHARGING
BuvinJ
  • 7,867
  • 3
  • 61
  • 78