Get an alert when my battery reaches 95%

So I haven't totally given up on blogging - just that I have other work to do as well. But this was an interesting 5-minute scripting challenge, nothing what-so-ever to do with Hyper-V. Someone internally asked whether it was possible to get an alert when their laptop battery reached 95% to remind them to unplug it. (For some reason, they wanted to keep the battery between 15% and 95% - I'm not a lithium battery expert, but I always thought it was best to fully charge then fully discharge them. Anyway, reasons aside....). On the particular laptop, the OEM software didn't have a configuration utility to say something like "do not charge if battery level is above X%". Hence, a quick and dirty script to the rescue.

Save below as battery.vbs and put a shortcut to "wscript path-tobattery.vbs" in your startup folder. (I will attach it to once I figure out what's going on with the blog engine!!). Obviously it won't work accurately if you have multiple batteries, and I'm sure someone can PowerShell-ify it down to far less code. There's a challenge for someone...


set oLocator = CreateObject("WbemScripting.SWbemLocator")
set oServices = oLocator.ConnectServer(".","rootwmi")
set oResults = oServices.ExecQuery("select * from batteryfullchargedcapacity")
for each oResult in oResults
   iFull = oResult.FullChargedCapacity

while (1)
  set oResults = oServices.ExecQuery("select * from batterystatus")
  for each oResult in oResults
    iRemaining = oResult.RemainingCapacity
    bCharging = oResult.Charging
  iPercent = ((iRemaining / iFull) * 100) mod 100
  if bCharging and (iPercent > 95) Then msgbox "Battery is at " & iPercent & "%",vbInformation, "Battery monitor"
  wscript.sleep 30000 ' 5 minutes

Comments (1)

  1. Moore-met caffe says:

    Welcome back ! What about some links from hyperv to cloud os !

Skip to main content