Q: My laptop stops charging sometimes, what do I do?
Published Jul 27 2023 03:00 AM 2,490 Views
Microsoft

Old computers often work fine but they develop trouble charging. I had my laptop in a docking station today as it has been all week, but it ran out of power and turned off in the middle of a meeting. I immediately scrambled for my personal smart phone and got back into the meeting. My personal cell phone was already connected through Intune to my company and had Teams already installed and configured so it was quick and easy to recover, but hard to present what had just been lost on my laptop.

Fortunately, my colleagues were understanding but I don’t want my laptop powering off unexpectedly when I just need to wiggle the cable to keep it going. Like most problems, this one can be solved with a script. This example shows how to leverage PowerShell in your daily life without going out and writing big complex scripts.

Disclaimer

The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

I didn’t just happen to know how to check battery status from PowerShell so I Binged it to find the WMI object here https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-battery. Then I ran the following to see what my battery looked like.

Get-WmiObject win32_battery

 

I examined the output and quickly learned my laptop has 2 batteries. I decided I wanted to be alerted if either battery was below 50% or was considered low or critical. If that was the case, then give me a popup so that I know that I need to wiggle my cable.

$batteryStatus = Get-WmiObject win32_battery

$badStatus = @(4,5,8,9)

$minChargeRemaining = 50



ForEach($battery in $batteryStatus){

    If($battery.BatteryStatus -in $badStatus -or $battery.EstimatedChargeRemaining -lt $minChargeRemaining){

        [System.Windows.MessageBox]::Show("Your computer has an estimated $($battery.EstimatedChargeRemaining) minutes remaining. Please wiggle the charging cable.")

    }

}

Yay! Now when I run that script it will alert me if my battery is low but not bother me if the battery is fine. I could also just look at my battery icon every 5 minutes, but that is easy to forget when I’m working so I put it in a loop. Now my computer will let me know if I need to give the power cable a little wiggle.

 

The PowerShell script is available at https://github.com/PaulHCode/PowerWiggler

You might say that my workaround is a bit kludgy and not a permanent solution. You’re right, but it is enough to keep the laptop in service until I can get a new one. I like perfect permanent solutions but sometimes we need a stop-gap measure and in this case, PowerShell is my duct tape to keep everything working for just a little while longer.

 

Have fun scripting!

 

References:

1 Comment
Co-Authors
Version history
Last update:
‎Jul 27 2023 03:00 AM
Updated by: