1

So, I just want to know how to get the battery percentage in Batch.
I think it would be good if the format was like this:

:forever
get-battery
if "%battery%"=="100%" goto reached100
goto forever

:reached100  
echo Your battery has finished charging!
goto forever
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
zulc22
  • 337
  • 2
  • 7
  • 17

4 Answers4

4

scientist_7's answer should be marked correct.

Of course, there's no law against calling powershell from a batch.

powershell -command "(Get-WmiObject Win32_Battery).EstimatedChargeRemaining"
56
Paul Williams
  • 3,099
  • 38
  • 34
  • 1
    With Powershell 6 and beyond (i.e. Powershell Core), you can use `(Get-CimInstance Win32_Battery).EstimatedChargeRemaining`. More details on Get-CimInstance at https://stackoverflow.com/questions/54495023/what-library-for-powershell-6-contains-the-get-wmiobject-command – Ehtesh Choudhury Jan 27 '21 at 03:46
2

Using WMIC

:: Variables to translate the returned BatteryStatus integer to a descriptive text
SET BatteryStatus.1=discharging
SET BatteryStatus.2=The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging.
SET BatteryStatus.3=fully charged
SET BatteryStatus.4=low
SET BatteryStatus.5=critical
SET BatteryStatus.6=charging
SET BatteryStatus.7=charging and high
SET BatteryStatus.8=charging and low
SET BatteryStatus.9=charging and critical
SET BatteryStatus.10=UNDEFINED
SET BatteryStatus.11=partially charged

:: Read the battery status
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Battery Get BatteryStatus /Format:List ^| FIND "="') DO SET %%A

:: Check the battery status, and display a warning message if running on battery power
IF NOT "%BatteryStatus%"=="2" (
    > "%~dpn0.vbs" ECHO MsgBox vbLf ^& "The laptop is currently running on its battery." ^& vbLf ^& vbLf ^& "The battery is !BatteryStatus.%BatteryStatus%!." ^& vbLf ^& vbLf ^& "Connect the laptop to the mains voltage if possible." ^& vbLf ^& " "^, vbWarning^, "Battery Warning"
    CSCRIPT //NoLogo "%~dpn0.vbs"
    DEL "%~dpn0.vbs"
)

Check the complete script at http://www.robvanderwoude.com/files/battrun_xp.txt

RealHowTo
  • 34,977
  • 11
  • 70
  • 85
  • 1
    I will never understand why people prefer to write [arrays in Batch files](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) in an unusual way, that gives them a strange and cryptic look that frequently requires an explanation, instead of use the square-braquets standard way that is commonly used in all the world: `SET BatteryStatus[1]=discharging`; see: [this comment](http://stackoverflow.com/questions/10544646/dir-output-into-bat-array/10569981#10569981). – Aacini Nov 07 '14 at 05:44
  • I want percentage. like: `100` (100%) or `2` (2%), So i could use it in "IF" commands. – zulc22 Nov 08 '14 at 04:06
  • 2
    Instead of `BatteryStatus`, try with `EstimatedChargeRemaining` – RealHowTo Nov 08 '14 at 14:41
  • Also - I'm very confused with this. – zulc22 Dec 07 '14 at 21:13
1

You Can Check It Via Wmic Command.

EDIT:

:forever
Goto get-battery
Echo "%Ba%"
:Next
if "%Ba%"=="100" Goto Fin
goto forever

:Fin
echo Your battery has finished charging!
pause>nul
exit

:get-battery
for /f "tokens=2 delims==" %%E in ('wmic path Win32_Battery get EstimatedChargeRemaining /value') do (set "Ba=%%E")
Goto Next
0

Just adding to this, I needed to make sure a batch script won't run on a laptop with a low battery, but also work for desktops without a battery.

SET BatteryPercent=100
FOR /F "tokens=2 delims==" %%i in ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Value 2^>^&1') DO SET BatteryPercent=%%i
IF %BatteryPercent% LSS 20 (EXIT)

So this sets the battery variable to 100 then overwrites it with the actual battery value, so that desktops will report 100. The weird bit after /Value is just to suppress the battery device not found error on desktops.

WhoIsRich
  • 4,053
  • 1
  • 33
  • 19