-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatteryMonitor.ps1
More file actions
64 lines (53 loc) · 2.5 KB
/
Copy pathBatteryMonitor.ps1
File metadata and controls
64 lines (53 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# =====================================================
# Battery Monitor Script - Runs in Background
# Shows alert when:
# - Charging & battery >= 95% → "Battery Full"
# - Not charging & battery <= 20% → "Battery Low"
# Alerts only once per event. Resets when condition clears.
# =====================================================
$fullThreshold = 95
$lowThreshold = 20
$checkIntervalSec = 120 # Check every 2 minutes
$notifiedFull = $false
$notifiedLow = $false
Write-Host "Battery monitor started. Checking every $checkIntervalSec seconds..." -ForegroundColor Green
while ($true) {
try {
# Get battery info
$battery = Get-WmiObject -Class Win32_Battery -ErrorAction Stop
if (-not $battery) { Start-Sleep -Seconds 10; continue }
$percent = $battery.EstimatedChargeRemaining
$status = $battery.BatteryStatus # 1=Discharging, 2=Charging, 3=Fully Charged
$charging = ($status -eq 2) -or ($status -eq 3)
$fullyCharged = ($status -eq 3)
# Debug (optional): Uncomment to see values
# Write-Host "Battery: $percent% | Status: $status | Charging: $charging"
# Alert: Battery Full (Charging and >= 95%)
if ($charging -and $percent -ge $fullThreshold -and -not $notifiedFull) {
[System.Media.SystemSounds]::Asterisk.Play()
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Battery is now $percent%. Please unplug charger to preserve battery life.", 0, "Battery Full Boss", 0x40)
$notifiedFull = $true
}
# Reset full alert when unplugged or drops below threshold
elseif (-not $charging -or $percent -lt ($fullThreshold - 5)) {
$notifiedFull = $false
}
# Alert: Battery Low (Not charging and <= 20%)
if (-not $charging -and $percent -le $lowThreshold -and -not $notifiedLow) {
[System.Media.SystemSounds]::Exclamation.Play()
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Battery is low: $percent%. Please plug in the charger.", 0, "Battery Low Boss !!!", 0x30)
$notifiedLow = $true
}
# Reset low alert when charging or above 25%
elseif ($charging -or $percent -gt ($lowThreshold + 5)) {
$notifiedLow = $false
}
}
catch {
# WMI might fail temporarily (rare), just wait and retry
Write-Warning "Failed to read battery info: $($_.Exception.Message)"
}
Start-Sleep -Seconds $checkIntervalSec
}