Skip to main content

How to Check if a Laptop is on AC Power or Battery in Batch Script

For scripts that perform long-running or resource-intensive tasks (like large backups, software compilations, or video rendering), it's a crucial best practice to first check the computer's power source. Running a high-CPU task on a laptop that is on battery power can unexpectedly drain the battery and cause the operation to fail or the system to shut down.

This guide will teach you how to use the built-in WMIC (Windows Management Instrumentation Command-line) utility to query the battery's status from a batch script. This allows you to create "power-aware" scripts that can decide whether to proceed with a task based on the power source.

The Core Command: WMIC Path Win32_Battery

Windows does not have a simple environment variable like %POWER_STATUS%. To get this information, we must query the system's hardware state. The WMIC utility provides a direct interface to this information. The specific class that holds the battery information is Win32_Battery.

To get the specific status, we use the command: WMIC Path Win32_Battery GET BatteryStatus

The BatteryStatus Property Explained

The BatteryStatus property returns a number that represents the current state of the battery. The most important values for our purpose are:

CodeMeaningPower Source
1DischargingBattery
2On AC, Not Charging*AC Power
3-9Charging, Low, Critical, etc.AC Power

This is the most common value when plugged in and the battery is full. Other values (like 3, 6, 7, 8, 9) also indicate that the laptop is connected to AC power because it is charging.

From this, we can derive a simple rule: If BatteryStatus is 1, the laptop is on battery. For any other value, it is on AC power.

Basic Example: A Simple Power Status Check

This script runs the WMIC command and uses a series of IF statements to interpret the result.

@ECHO OFF
SET "Status="

REM The skip=1 ignores the header line ("BatteryStatus").
FOR /F "skip=1" %%S IN ('WMIC Path Win32_Battery GET BatteryStatus') DO (
SET "Status=%%S"
GOTO :StatusFound
)

:StatusFound
ECHO --- Power Status Check ---

IF NOT DEFINED Status (
ECHO This computer does not appear to have a battery (e.g., a desktop).
GOTO :EOF
)

IF %Status% EQU 1 (
ECHO [STATUS] The computer is currently running on BATTERY power.
) ELSE (
ECHO [STATUS] The computer is currently on AC POWER.
)

How to Capture the Status in a Variable

The output of WMIC is not just the number; it includes a header and extra lines, making it tricky to parse.

BatteryStatus
2


The FOR /F "skip=1" loop is the standard and most effective way to capture this value:

  • FOR /F: The text-parsing loop.
  • skip=1: This tells the loop to ignore the first line of the command's output, which is the "BatteryStatus" header.
  • %%S IN (...): The loop variable %%S will be assigned the value from the first non-skipped line (the number 2).
  • GOTO :StatusFound: Because WMIC can output extra blank lines, we use GOTO to exit the loop immediately after we've captured the first value, preventing our variable from being overwritten with an empty line.

Common Pitfalls and How to Solve Them

Problem: The Script is Run on a Desktop Computer

If you run this WMIC query on a desktop PC that has no battery, the command will fail because the Win32_Battery class does not exist.

Example of error:

No Instance(s) Available.

In this case, the FOR /F loop will not find anything to process, and your Status variable will never be defined.

Solution: Check with IF DEFINED

The example script above already demonstrates the solution. After the loop, you must check if your variable was successfully set.

IF NOT DEFINED Status (
ECHO This computer does not appear to have a battery.
)

For the purpose of a power check, you can usually assume that a computer without a battery is running on AC power.

Problem: The WMIC Output Includes a Header and Blank Lines

This is the primary parsing challenge.

Solution: The FOR /F "skip=1" combined with an immediate GOTO is the most robust way to handle this. It reliably isolates the single line of output that contains the status code.

Practical Example: A "Pre-Flight Check" for a Long Task

This script is designed to run before a long, power-intensive backup. It checks the power source and will only proceed if the laptop is plugged into AC power.

@ECHO OFF
SETLOCAL
SET "PowerStatus="

ECHO --- Backup Pre-Flight Check ---
ECHO Checking power source before starting the backup...

FOR /F "skip=1" %%S IN ('WMIC Path Win32_Battery GET BatteryStatus') DO (
SET "PowerStatus=%%S"
GOTO :CheckPower
)

:CheckPower
REM If PowerStatus is not defined, it's a desktop. Assume AC power.
IF NOT DEFINED PowerStatus SET "PowerStatus=2"

IF %PowerStatus% EQU 1 (
ECHO.
ECHO [FAILURE] The laptop is on battery power.
ECHO Please connect to AC power before running a long backup.
ECHO Backup aborted.
EXIT /B 1
)

ECHO.
ECHO [SUCCESS] The computer is on AC power.
ECHO Starting backup process now...
REM (Your Robocopy or other backup commands would go here)

ENDLOCAL

Conclusion

Checking the power source is a key part of writing "responsible" batch scripts that won't cause unexpected problems for users on mobile devices.

The key takeaways are:

  • Use the WMIC Path Win32_Battery GET BatteryStatus command to query the battery state.
  • A BatteryStatus of 1 means the device is on battery power; any other value typically means it is on AC power.
  • Use a FOR /F "skip=1" loop with a GOTO to reliably capture the status code into a variable.
  • Always check if your variable was defined to gracefully handle desktop computers that have no battery.