Skip to main content

How to Get Battery Status and Percentage in Batch Script

For scripts running on laptops or other portable devices, being aware of the battery level is a critical part of robust automation. A script that starts a long, power-intensive process on a device with a low battery is likely to fail when the device runs out of power. By querying the battery status, your script can make intelligent decisions, such as warning the user or postponing a task until the device is charging.

This guide will teach you how to use the built-in WMIC (Windows Management Instrumentation Command-line) utility to get detailed information about the battery, including its remaining charge percentage and whether it's currently charging.

The Core Command: WMIC Path Win32_Battery

Windows does not store battery information in a simple environment variable. To get this data, we must query the system's hardware state using WMIC. The specific WMI class that contains battery information is Win32_Battery.

To get a list of all available properties for the battery, you can run: WMIC Path Win32_Battery GET

Key Battery Properties Explained

The Win32_Battery class has many properties, but for most scripting needs, two are essential:

  • EstimatedChargeRemaining: This is the most useful property. It returns a number from 0 to 100, representing the percentage of battery life remaining.
  • BatteryStatus: This property returns a number code that tells you what the battery is currently doing (e.g., charging, discharging). This is how you can tell if the laptop is plugged into AC power.

Basic Example: Getting All Battery Information

This script uses WMIC to get the two most important properties and display them.

@ECHO OFF
ECHO --- Querying Battery Status ---
WMIC Path Win32_Battery GET EstimatedChargeRemaining, BatteryStatus

The output is formatted in columns, with a header and some extra trailing lines.

--- Querying Battery Status ---
BatteryStatus EstimatedChargeRemaining
2 98

Capturing Specific Values (Charge and Status) into Variables

To use this information in a script, we need to capture the values from the WMIC output. A FOR /F loop is the standard tool for this, but the parsing can be tricky due to WMIC's output format.

@ECHO OFF
SETLOCAL
SET "BatteryCharge="
SET "BatteryStatus="

ECHO --- Capturing Battery Details ---

REM 'skip=1' ignores the header line. 'tokens=1,2' grabs the two columns.
FOR /F "skip=1 tokens=1,2" %%A IN (
'WMIC Path Win32_Battery GET BatteryStatus, EstimatedChargeRemaining'
) DO (
SET "BatteryStatus=%%A"
SET "BatteryCharge=%%B"
GOTO :ValuesCaptured
)

:ValuesCaptured
IF NOT DEFINED BatteryCharge (
ECHO This computer does not appear to have a battery.
GOTO :End
)

ECHO.
ECHO Charge Percentage: %BatteryCharge%%%
ECHO Status Code: %BatteryStatus%

:End
ENDLOCAL
  • skip=1: Ignores the BatteryStatus EstimatedChargeRemaining header.
  • tokens=1,2: Specifies that we want to capture the first two "words" (columns) of the output.
  • GOTO :ValuesCaptured: Exits the loop immediately after the first line of data is captured to avoid having the variables overwritten by blank lines.

How to Interpret the BatteryStatus Code

The BatteryStatus code tells you if the device is on AC power or battery.

CodeMeaningPower Source
1DischargingBattery Power
2On AC (Not Charging, Fully Charged)AC Power
3-9Charging, Low, Critical, etc.AC Power

A simple rule for scripting: If BatteryStatus is 1, it's on battery. Otherwise, it's plugged in.

Common Pitfalls and How to Solve Them

Problem: The Script is Run on a Desktop Computer

If your script runs on a desktop PC with no battery, the WMIC command will fail because the Win32_Battery object doesn't exist.

Example of error message:

No Instance(s) Available.
note

In this case, the FOR /F loop will find nothing to process, and your variables will remain undefined.

Solution: Check with IF DEFINED

The example script in section before already demonstrates this. After the loop, a simple IF NOT DEFINED BatteryCharge check is all you need to gracefully handle this scenario. For power-checking purposes, you can safely assume a computer without a battery is on AC power.

Problem: Parsing the WMIC Output is Tricky

The WMIC command adds extra carriage returns and blank lines that can cause a simple FOR /F loop to fail if not handled correctly.

Solution: The combination of skip=1 and an immediate GOTO out of the loop is the most robust pattern. It ensures you grab only the first line of data and ignore all the trailing empty lines.

Practical Example: A "Low Power" Pre-Flight Check Script

This script is designed to run before a long, resource-intensive task. It checks two conditions:

  1. Is the laptop on battery power?
  2. If so, is the remaining charge below a critical threshold (e.g., 50%)?
@ECHO OFF
SETLOCAL
SET "CHARGE_THRESHOLD=50"

ECHO --- Power Pre-Flight Check ---
SET "BatteryCharge="
SET "BatteryStatus="

FOR /F "skip=1 tokens=1,2" %%A IN ('WMIC Path Win32_Battery GET BatteryStatus, EstimatedChargeRemaining') DO (
SET "BatteryStatus=%%A"
SET "BatteryCharge=%%B"
GOTO :CheckPower
)

:CheckPower
REM A desktop PC is assumed to have sufficient power.
IF NOT DEFINED BatteryCharge (
ECHO Desktop computer detected. Assuming AC power.
GOTO :Proceed
)

ECHO Laptop detected. Status: %BatteryStatus%, Charge: %BatteryCharge%%%

REM --- The Logic Check ---
REM Check if the laptop is on battery (Status=1) AND the charge is below the threshold.
IF %BatteryStatus% EQU 1 IF %BatteryCharge% LSS %CHARGE_THRESHOLD% (
ECHO.
ECHO [FAILURE] Low battery warning!
ECHO The laptop is on battery and the charge is below %CHARGE_THRESHOLD%%%.
ECHO Please connect to AC power before starting.
GOTO :Abort
)

:Proceed
ECHO.
ECHO [SUCCESS] Power level is sufficient.
ECHO Starting the main task now...
GOTO :End

:Abort
ECHO Operation aborted.

:End
ENDLOCAL

Conclusion

Querying the battery status is a key technique for writing "responsible" scripts that won't cause data loss or failures by running on a device with low power.

Key takeaways:

  • Use WMIC Path Win32_Battery GET EstimatedChargeRemaining, BatteryStatus to get the core data.
  • Use a FOR /F "skip=1 tokens=1,2" ... GOTO loop to reliably capture the charge and status into variables.
  • Remember that a BatteryStatus of 1 means the device is on battery.
  • Always check IF NOT DEFINED on your variables to gracefully handle desktop PCs.