Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if computer is plugged into AC power in batch file

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?

like image 740
Jakob Weisblat Avatar asked Sep 08 '11 22:09

Jakob Weisblat


1 Answers

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.

like image 199
Rakha Avatar answered Oct 30 '22 14:10

Rakha