How can I set the output value of a WMIC command to check the notebook battery as a batch variable?

Viewed 38

I want to read out details about the battery as follows:

WMIC PATH Win32_Battery Get EstimatedChargeRemaining

Can I then display the output of this using the %value% variable, for example?

1 Answers

A batch file for this task is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "EstimatedChargeRemaining="
for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_Battery GET EstimatedChargeRemaining /VALUE 2^>nul') do set "EstimatedChargeRemaining=%%I"
if defined EstimatedChargeRemaining echo Estimated remaining battery charge: %EstimatedChargeRemaining% %%
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic path /?
  • wmic path win32_battery /?
  • wmic path win32_battery get /?

See also the Microsoft documentation page for the Win32_Battery class.

Related