Script to retrieve AC & Battery usage

Hello all,

Im trying to identify a way to construct a PS script that could pull out percentage of AC & Battery usage for a given w10 system.
While searching for info and the way to run this, I manage to use some code that looks like follows, but I missing all the rest of it in order t oget exactly what Im looking for:

At this point I manage to find 2 ways to sort this out as follows:

  1. I try using powercfg, great tool, check command bellow:
    powercfg /batteryreport /output c:\temp\batteryreport.xml /xml
    My porblem with it, its that the output of the xml data is not consumable, lots of codes that only MS knows, HTML nice one, but I can’t find a way to search for the coorect info and inject it as a variable for ex.

  2. Get-WinEvent to get all 105 ID event where plug and unplug AC data is avalible but Im lost in which way or script adoption is needed to get those number:

You can get the event raw xml and filter the properties you need:

$events = Get-WinEvent -FilterHashtable @{logname=“System”; id=105}

get the first event raw XML

$event = [xml]$events[0].ToXml()

display its content

$event.Event.EventData.Data
$event

Now, at this point the only way I found to find some info itas at option 2, but I dont find a way to put the needed info into a variable so I can then start the calculation part to get porcentages of used type.

Any help is really wellcome, thanks in advance

Not sure why flagged, is it because of the links to the Microsoft Docs for the classes?

Hi,
You may want to pull the information from WMI on the system then you will have something you can search easily.

WMI Classes: Win32_Battery OR Win32_PortableBattery

Links below explain what some of the values mean:
Win32_Battery class - Win32 apps | Microsoft Docs
Win32_PortableBattery class - Win32 apps | Microsoft Docs

$query1 = "select * from Win32_Battery"
$query2 = "Select * from Win32_PortableBattery"

#Create Has tables to get information
$test1 = {}
$test2 = {}

$test1=Get-WmiObject -Query $query1
$test2=Get-WmiObject -Query $query2

#not sure what the differences are between the classes

Just to mention it at least once … Get-WmiObject is deprecated and should no longer be used. And it is not available anymore in PowerShell 6 and above.
Instead use Get-CimInstance.

So your code suggestion would be “translated” to:

Get-CimInstance -ClassName Win32_Battery
Get-CimInstance -ClassName Win32_PortableBattery

… or maybe liek this:

Get-CimInstance -ClassName CIM_Battery

… but I cannot explain what exactly the difference is. :wink:

@Olaf Thanks, I wasn’t aware about it being depricated

The Doc links above do show there are some feature only available in CIM_Battery and the link to the CIM_Battery class is: CIM_Battery class - Win32 apps | Microsoft Docs

1 Like