8

I need to create a task in TS based on battery level change. Let's say my battery drops from 67% to 66%. How can I run a task based on this event. Does Windows log this at all? I couldn't find this information anywhere.

  • Which version of Windows? – DavidPostill Jan 2 '17 at 14:44
  • WIndows 10 (and preferably W7 as well) – RiddleMeThis Jan 2 '17 at 14:49
10

I need to create a task in Task Scheduler based on battery level change

Windows doesn't log this kind of detail as events. You can however use something like the batch file below and create a custom event.


Battery.cmd

This batch file monitors the current battery percentage charge and creates a user defined event if the charge drops below a user defined threshold value.

@echo off
setlocal EnableDelayedExpansion
rem set threshold value
set _threshold=82
:start
rem get the battery charge
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1" %%i in (`wmic Path Win32_Battery Get EstimatedChargeRemaining ^| findstr /r /v "^$"`) do (
  set _charge=%%i
  echo !_charge!
  if !_charge! lss !_threshold! (
    echo threshold reached
    rem create a custom event in the application event log
    rem requires administrator privileges 
    eventcreate /l APPLICATION /t WARNING /ID 999 /D "Battery charge has dropped"
    goto :done
    ) else (
    rem wait for 10 minutes then try again
    timeout /t 600 /nobreak
    goto :start
    )
  )
:done
endlocal

Notes:

  • The Eventcreate command works on Windows XP up to and including Windows 10, it requires administrator privileges to work
  • Set _threshold as required
  • If the battery falls below this value an event with ID 999 will be generated in the APPLICATION event log with the description Battery charge has dropped
  • Modify the eventcreate command as required for your situation.
  • Modify the timeout delay as required for your situation.

Example output:

My battery currently has a charge of 81%. I set the threshold to 82. Here is what happens when I run Battery.cmd:

> battery
81
threshold reached

SUCCESS: An event of type 'WARNING' was created in the 'APPLICATION' log with 'EventCreate' as the source.

And here is the new entry in the Event Log:

enter image description here


eventcreate syntax

EVENTCREATE [/S system [/U username [/P [password]]]] /ID eventid
            [/L logname] [/SO srcname] /T type /D description

Description:
    This command line tool enables an administrator to create
    a custom event ID and message in a specified event log.

Parameter List:
    /S    system           Specifies the remote system to connect to.

    /U    [domain\]user    Specifies the user context under which
                           the command should execute.

    /P    [password]       Specifies the password for the given
                           user context. Prompts for input if omitted.

    /L    logname          Specifies the event log to create
                           an event in.

    /T    type             Specifies the type of event to create.
                           Valid types: SUCCESS, ERROR, WARNING, INFORMATION.

    /SO   source           Specifies the source to use for the
                           event (if not specified, source will default
                           to 'eventcreate'). A valid source can be any
                           string and should represent the application
                           or component that is generating the event.

    /ID   id               Specifies the event ID for the event. A
                           valid custom message ID is in the range
                           of 1 - 1000.

    /D    description      Specifies the description text for the new event.

    /?                     Displays this help message.


Examples:
    EVENTCREATE /T ERROR /ID 1000
        /L APPLICATION /D "My custom error event for the application log"

    EVENTCREATE /T ERROR /ID 999 /L APPLICATION
        /SO WinWord /D "Winword event 999 happened due to low diskspace"

    EVENTCREATE /S system /T ERROR /ID 100
        /L APPLICATION /D "Custom job failed to install"

    EVENTCREATE /S system /U user /P password /ID 1 /T ERROR
        /L APPLICATION /D "User access failed due to invalid user credentials"

Further Reading

6

There is a Microsoft-Windows-Battery ETW provider with BatteryPercentRemaining event with ID 13. You can code a project that uses TraceEvent to create a realtime listener for this Microsoft-Windows-Battery provider. The event has the entries RemainingPercentage to show the status and PercentageChange to see the change:

enter image description here

When you see this event and see the -1 change for PercentageChange, run the program you want.

2

OK, the script provided by DavidPostill does not work. It's nice little script, but the code is either erratic or outdated.

Here is the fixed one:

@echo off
setlocal EnableDelayedExpansion
rem set threshold value
set _threshold=30
:start
rem get the battery charge
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1" %%i in (`wmic Path Win32_Battery Get EstimatedChargeRemaining ^| findstr /r /v "^$"`) do (
  set _charge=%%i
  echo !_charge!
  if !_charge! lss !_threshold! (
    echo threshold reached
    rem create a custom event in the application event log
    rem requires administrator privileges
    eventcreate /l APPLICATION /t WARNING /ID 999 /D "Battery charge has dropped below the threshold."
    goto :done
  ) else (
    rem wait for 1 minute then try again
    timeout /t 60 /nobreak
    goto :start
  )
)
:done
endlocal

I suggested this edit into DavidPostill's answer, but I don't know why it wasn't approved...

  • He set me on the right track, which is why I accepted his answer, but thank you very much! for your fixed script. – RiddleMeThis Jan 9 '17 at 9:08
  • While fixing the code, I came to realise how buggy and erroneous the findstr is... Way too bad! Seriuosly, Microsoft? I'm impressed by DavidPostill's dirty little hack to get the job done. – AneesAhmed777 Jan 9 '17 at 14:43
0

There is a much easier way to check battery level. In navigation area just put mouse over the battery icon and it will give a percentage.

  • Yes,but thats really not what I wanted... I wanted to monitor battery level automatically and based on battery level do some stuff (again automatically) – RiddleMeThis Jan 21 '17 at 19:20
  • Please read the question again carefully. Your answer does not answer the original question. – DavidPostill Jan 21 '17 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.