Created
March 8, 2025 06:37
-
-
Save tusharsnx/1e6a8f82d16e87d5ecec4b3a76be1f58 to your computer and use it in GitHub Desktop.
Notification for battery charge limit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Show-Notification { | |
[cmdletbinding()] | |
Param ( | |
[string] | |
$ServiceName, | |
[string] | |
$ToastTitle, | |
[string] | |
[parameter(ValueFromPipeline)] | |
$ToastText | |
) | |
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null | |
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02) | |
$Xml = [xml] $Template.GetXml() | |
($Xml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($Xml.CreateTextNode($ToastTitle)) > $null | |
($Xml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($Xml.CreateTextNode($ToastText)) > $null | |
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument | |
$SerializedXml.LoadXml($Xml.OuterXml) | |
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml) | |
$Toast.Tag = $ServiceName | |
$Toast.Group = $ServiceName | |
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1) | |
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($ServiceName) | |
$Notifier.Show($Toast); | |
} | |
function Start-Service { | |
$ChargingThreshold = 80; | |
$CheckInterval = 3 * 60; # 3 minutes | |
$ServiceName = "Charging Service" | |
$ToastText = "You can unplug the charger now" | |
do { | |
$Charging = $(Get-WmiObject -Class batterystatus -Namespace root\wmi).Charging | |
$BatteryLevel = (Get-WmiObject win32_battery).EstimatedChargeRemaining | |
if ($Charging -eq $true -and $BatteryLevel -ge $ChargingThreshold) { | |
$ToastTitle = "Battery level reached $BatteryLevel%" | |
Show-Notification -ServiceName $ServiceName -ToastTitle $ToastTitle -ToastText $ToastText | |
} | |
Start-Sleep -Seconds $CheckInterval | |
} while($true) | |
} | |
Start-Service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment