Powershell: Scripts to check battery levels
coding battery scheduled-tasks powershell
Some example Powershell scripts for running one script as a scheduled task. The example is to check battery levels every 10 minutes for 4 hours, but the same approach can be used for any script you want to run periodically.
My “laptop” is a Surface Book 2 getting towards end of life. Its has 2 batteries - one in the base and one in the screen. The battery has be a bit errant of late shutting system down unnecessarily. I want to check the battery levels every 10 minutes for a few hours to see how they are doing. I could do this manually but it is easier to automate it with a script and scheduled task.
Script to run periodically
At c:\temp\batt.ps1
$logFile = "C:\temp\batt.log"
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$batteries = Get-CimInstance -ClassName Win32_Battery
foreach ($b in $batteries) {
$status = switch ($b.BatteryStatus) {
1 { "Discharging" }
2 { "Charging" }
3 { "Fully Charged" }
default { "Unknown ($($b.BatteryStatus))" }
}
$line = "$timestamp | Battery $($b.DeviceID): $($b.EstimatedChargeRemaining)% - $status"
Add-Content -Path $logFile -Value $line
}
The Task Scheduler script
Run this from an elevated PowerShell prompt to create the scheduled task. It will run the above script every 10 minutes for 4 hours, logging the battery status to a file.
$action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-NoProfile -ExecutionPolicy Bypass -File C:\temp\batt.ps1"
$trigger = New-ScheduledTaskTrigger `
-Once -At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes 10) `
-RepetitionDuration (New-TimeSpan -Hours 4)
$settings = New-ScheduledTaskSettingsSet `
-WakeToRun `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries
Register-ScheduledTask `
-TaskName "BatteryMonitor10Min_4Hour" `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Description "Logs battery state every 10 minutes for 4 hours"
Responese from the above:
> TaskPath TaskName State
-------- -------- -----
\ BatteryMonitor10Min_4Hour Ready
Sample Log Output
2026-06-14 17:53:23 | Battery 3002896806SMPM1030906: 98% - Charging
2026-06-14 17:53:23 | Battery 8030163831SMPM1030911: 78% - Charging
2026-06-14 18:03:21 | Battery 3002896806SMPM1030906: 98% - Charging
2026-06-14 18:03:21 | Battery 8030163831SMPM1030911: 85% - Charging
2026-06-14 18:13:21 | Battery 3002896806SMPM1030906: 100% - Charging
2026-06-14 18:13:21 | Battery 8030163831SMPM1030911: 90% - Charging
2026-06-14 18:23:20 | Battery 3002896806SMPM1030906: 100% - Charging
2026-06-14 18:23:20 | Battery 8030163831SMPM1030911: 93% - Charging
2026-06-14 18:33:20 | Battery 3002896806SMPM1030906: 100% - Charging
2026-06-14 18:33:20 | Battery 8030163831SMPM1030911: 100% - Charging
2026-06-14 19:01:01 | Battery 3002896806SMPM1030906: 100% - Charging
2026-06-14 19:01:01 | Battery 8030163831SMPM1030911: 100% - Charging
2026-06-14 19:11:02 | Battery 3002896806SMPM1030906: 100% - Charging
2026-06-14 19:11:02 | Battery 8030163831SMPM1030911: 100% - Charging
2026-06-14 19:21:01 | Battery 3002896806SMPM1030906: 100% - Charging
2026-06-14 19:21:01 | Battery 8030163831SMPM1030911: 100% - Charging
2026-06-14 19:31:01 | Battery 3002896806SMPM1030906: 100% - Charging
2026-06-14 19:31:01 | Battery 8030163831SMPM1030911: 100% - Charging
2026-06-14 19:41:02 | Battery 3002896806SMPM1030906: 100% - Discharging
2026-06-14 19:41:02 | Battery 8030163831SMPM1030911: 100% - Discharging
2026-06-14 20:21:58 | Battery 3002896806SMPM1030906: 100% - Discharging
2026-06-14 20:21:58 | Battery 8030163831SMPM1030911: 100% - Discharging
2026-06-14 20:31:03 | Battery 3002896806SMPM1030906: 91% - Discharging
2026-06-14 20:31:03 | Battery 8030163831SMPM1030911: 90% - Discharging
2026-06-14 22:11:01 | Battery 3002896806SMPM1030906: 85% - Discharging
2026-06-14 22:11:01 | Battery 8030163831SMPM1030911: 84% - Discharging
2026-06-14 22:52:08 | Battery 3002896806SMPM1030906: 80% - Discharging
2026-06-14 22:52:08 | Battery 8030163831SMPM1030911: 79% - Discharging
Check Task Status
> Get-ScheduledTask -TaskName "BatteryMonitor1Min_1Hour" | Get-ScheduledTaskInfo
LastRunTime : 14/06/2026 6:33:18 PM
LastTaskResult : 0
NextRunTime : 14/06/2026 6:43:17 PM
NumberOfMissedRuns : 0
TaskName : BatteryMonitor10Min_4Hour
TaskPath : \
PSComputerName :
Task Descheduler
> Unregister-ScheduledTask -TaskName "BatteryMonitor1Min_1hour" -Confirm:$false
Nb: Nothing shows for this but can now run the check again:
Check Task Status
> Get-ScheduledTask -TaskName "BatteryMonitor1Min_1Hour" | Get-ScheduledTaskInfo
Get-ScheduledTask : No MSFT_ScheduledTask objects found with property 'TaskName' equal to 'BatteryMonitor1Min_1Hour'.
Verify the value of the property and retry.
At line:1 char:1
+ Get-ScheduledTask -TaskName "BatteryMonitor1Min_1Hour" | Get-Schedule ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (BatteryMonitor1Min_1Hour:String) [Get-ScheduledTask], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound_TaskName,Get-ScheduledTask
Also ..
Pause the Tasl Scheduler
> Disable-ScheduledTask -TaskName "BatteryMonitor1Min_1Hour"
Continue the Task Scheduler
Enable-ScheduledTask -TaskName "BatteryMonitor1Min_1Hour"
Stop
Stop-ScheduledTask -TaskName "BatteryMonitor1Min_1Hour"
| Topic | Subtopic | |
| < Prev: | Code Clean Up | Specification of a tool to clean up code |
| This Category Links | ||
| Category: | Coding Index: | Coding |
| < Prev: | GitHub Copilot v Documentation | How far can you go with Copilot |