Laptop Battery — Full Charge Notification

Gibin Francis
2 min readJan 13, 2023

--

Photo by Panos Sakalakis on Unsplash

Recently I noticed that normal battery indicators in laptops using windows 10/11 will show only if it need a battery to be charged or if the battery is below certain level.

But in Some situations we may also need to be notified if the battery is fully charged, so that you can unplug your laptop and thus the battery will stay strong for long run ( its still a belief and new tech innovations may overcome this )

So I tried to find some solutions and you can also use if needed.

I Tried this solution as I don’t want to use any unknown application to run on my system for the same.

Step 1: Create a power shell script file with name and extension as “ToastNotification.ps1” and use the code snippet below

$ErrorActionPreference = "Stop"
$notificationTitle = "Battery fully charged"
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)

#Convert to .NET type for XML manipuration
$toastXml = [xml] $template.GetXml()
$toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null

#Convert back to WinRT type
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)

$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = "Battery"
$toast.Group = "Battery"
$toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(3)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("Battery")

$notifier.Show($toast);

exit $exitcode

Step 2: Create a VB Script file with extension “.vbs” and use the code snippet below


set oLocator = CreateObject("WbemScripting.SWbemLocator")
set oShell = CreateObject("Wscript.shell")
set oServices = oLocator.ConnectServer(".","root\wmi")
set oResults = oServices.ExecQuery("select * from batteryfullchargedcapacity")
for each oResult in oResults
iFull = oResult.FullChargedCapacity
next

while (1)
set oResults = oServices.ExecQuery("select * from batterystatus")
for each oResult in oResults
iRemaining = oResult.RemainingCapacity
bCharging = oResult.Charging
next
iPercent = ((iRemaining / iFull) * 100) mod 100
if bCharging and (iPercent > 97) Then oShell.run "powershell -executionpolicy bypass -file Your_Folder_path\ToastNotification.ps1", 0, True
wscript.sleep 300000 ' 5 minutes
wend

Step 3: Replace Your_Folder_path in step 2 with your folder path where you saved the step 1 file

Step 4 : Copy the VB Script file and paste in your startup folder

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

Now once you restart your laptop, VB Script will start running on background and the same will check if the battery is charging and also if the battery percentage is greater than 95. It will check the same on every 5 minutes.

Then it will call the notification PowerShell script to show you one toast notification.

You can adjust the battery percentage value and the delay time according to your need.

Hope this may help you.

Please find the repo here

Thanks for reading.. Happy coding..

--

--

Gibin Francis
Gibin Francis

Written by Gibin Francis

Technical guy interested in MIcrosoft Technologies, IoT, Azure, Docker, UI framework, and more

No responses yet