Using powershell to get a battery health report with output written to console or to text file

Brandon Poindexter 71 Reputation points
2021-12-16T21:44:04.7+00:00

As the title asks. I am aware that I can use powercfg /batteryreport to write out an HTML file with the information I'm looking for. I would like to get this information as text output in powershell if there is a way. I will be loading this information into a logging and alerting platform so we can be notified when laptop batteries are starting to need replacing.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Jay Hill 5 Reputation points
    2023-03-13T19:14:52.4766667+00:00

    A bit late to the conversation … but this is what I use to get the battery health of my laptops .

    
    $InfoAlertPercent = "70"
    $WarnAlertPercent = "50"
    $CritAlertPercent = "20"
    $BatteryHealth=""
    & powercfg /batteryreport /XML /OUTPUT "batteryreport.xml"
    Start-Sleep 1
    [xml]$b = Get-Content batteryreport.xml
    
    $b.BatteryReport.Batteries |
        ForEach-Object{
            [PSCustomObject]@{
                DesignCapacity = $_.Battery.DesignCapacity
                FullChargeCapacity = $_.Battery.FullChargeCapacity
                BatteryHealth = [math]::floor([int64]$_.Battery.FullChargeCapacity/[int64]$_.Battery.DesignCapacity*100)
                CycleCount = $_.Battery.CycleCount
                Id = $_.Battery.id
            }
    
            if (([int64]$_.Battery.FullChargeCapacity/[int64]$_.Battery.DesignCapacity)*100 -gt $InfoAlertPercent){
                $BatteryHealth="Great"
                write-host "Battery Health is $BatteryHealth"
            }elseif (([int64]$_.Battery.FullChargeCapacity/[int64]$_.Battery.DesignCapacity)*100 -and ([int64]$_.Battery.FullChargeCapacity/[int64]$_.Battery.DesignCapacity)*100 -gt $WarnAlertPercent){
                $BatteryHealth="OK"
                write-host "Battery Health is $BatteryHealth"
            }elseif (([int64]$_.Battery.FullChargeCapacity/[int64]$_.Battery.DesignCapacity)*100 -and ([int64]$_.Battery.FullChargeCapacity/[int64]$_.Battery.DesignCapacity)*100 -gt $CritAlertPercent){
                $BatteryHealth="Low"
                write-host "Battery Health is $BatteryHealth"
            }elseif (([int64]$_.Battery.FullChargeCapacity/[int64]$_.Battery.DesignCapacity)*100 -le $CritAlertPercent){
                $BatteryHealth="Critical"
                write-host "Battery Health is $BatteryHealth"
            }
        }
    
    
    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2021-12-16T22:18:25.66+00:00

    WMI might get you what you need:

    Get-WmiObject Win32_Battery
    Get-WmiObject -Class BatteryStaticData -Namespace ROOT\WMI
    Get-WmiObject -Class BatteryFullChargedCapacity -Namespace ROOT\WMI
    

    Or try something like this: proactive-battery-replacement-with-endpoint-analytics

    0 comments No comments

  3. Limitless Technology 39,916 Reputation points
    2021-12-23T20:59:43.23+00:00

    Hello @Brandon Poindexter

    By design powercfg output only allows HTML or XML formats: Link

    Hope this helps with your query,

    ----------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  4. Rich Matheisen 47,901 Reputation points
    2021-12-24T03:22:38.463+00:00

    Powercfg can produce an XML file, too. You can deal with that pretty easily. For example:

    [xml]$b = Get-Content C:\Junk\battery.xml
    
    $b.BatteryReport.Batteries |
        ForEach-Object{
            # if there are multiple batteries you'll get an array for each property (one entry for each battery)
            [PSCustomObject]@{
                DesignCapacity = $_.Battery.DesignCapacity
                FullChargeCapacity = $_.Battery.FullChargeCapacity
                CycleCount = $_.Battery.CycleCount
            }
        }
    

    The output looks like this (for a machine with two batteries):

    DesignCapacity FullChargeCapacity CycleCount
    -------------- ------------------ ----------
    {23200, 62200} {21720, 63590}     {5, 19}
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.