3

I am trying to plot a graph for Bluetooth headphone battery discharge. For that I need to read battery percentage of the connected device. I can see power information is available on GUI for the device. Is there any way to get the battery percentage info for connected Bluetooth device using PowerShell? (like using wmi or anything else)

Mahesh Mankar
  • 231
  • 3
  • 16

4 Answers4

6

In my findings, you can get information on Bluetooth devices using the Get-PnpDevice cmdlet. This should return a list of PnP Devices, their Status, Class, FriendlyName and InstanceID.

Get-PnpDevice

You can filter the results with -Class parameter. To specify Bluetooth PnP devices, you can enter "Bluetooth" as a string value for the -Class parameter.

Get-PnpDevice -Class 'Bluetooth'

You can then specify the device you have in mind from this list by their FriendlyName using the -FriendlyName parameter and enter the desired device's FriendlyName as a string value for the parameter.

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName'

Note: You can also specify the device using the -InstanceId parameter and providing the device's InstanceId as a string value for the parameter.

If you then pipe the previous command to the Get-PnpDeviceProperty cmdlet, it will return a list of the device's properties, including its InstanceId, KeyName, Type and Data.

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty

Beyond this point, I was able to further filter the results of the command by using the -KeyName parameter and entering the KeyName of the property that (I assume) contains Device Power Data as a string value for the parameter.

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty -KeyName 'PropertyKeyName'

Unfortunately this is as far as I've gotten to solving the problem. Hopefully my contribution helps.

Lockszmith
  • 2,173
  • 1
  • 29
  • 43
  • 2
    Great explanation and solution. I have an *MX Anywhere 2 mouse*, and it got a bit tricky for finding `KeyName` as it was just a key string (not a friendly name). After comparing the power info from Bluetooth panel against the values, I found the one corresponding to the battery %. Ended up as `(Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'MX Anywhere 2' | Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2').Data` in my case. Thanks! – Paulo Amaral May 20 '22 at 08:54
  • 1
    I have an Xbox One Controller and the KeyName for the battery is the same as @PauloAmaral ('{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2') – bkbilly Oct 20 '22 at 07:52
  • My headphones do not have any KeyName to indicate battery, but Windows shows battery. – steam3d Dec 05 '22 at 09:55
1

As far as I am aware, there is no way to poll bluetooth device data beyond what you would get with Get-WmiObject, since the battery status seen in Windows Settings -> Bluetooth Devices is something coming from the vendors/devices driver and seems to, as of now, be inaccessible by PowerShell, unless there is some exotic snapin I am not aware of.

You can get all possible device information via this command:

 Get-WmiObject -Query "select * from win32_PnPEntity" | Where Name -like "MyDeviceName"

Or if you are unsure how the device is named as of now, this would return a complete list of "devices":

 Get-WmiObject -Query "select * from win32_PnPEntity" | Select Name

Additionally, I couldn't find battery information in the registry - maybe someone more knowledge can expand on that because the registry probably contains the necessary information as it must be stored somewhere on the device.

Bowshock
  • 190
  • 5
1

Paulo Amaral's comment above is indeed the answer.

The code below will provide you with a command you can reuse to query the battery state of your device:

Get-PnpDevice -FriendlyName "*<Device Name>*" | ForEach-Object {
    $local:test = $_ |
    Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
        Where Type -ne Empty;
    if ($test) {
        "To query battery for $($_.FriendlyName), run the following:"
        "    Get-PnpDeviceProperty -InstanceId '$($test.InstanceId)' -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data"
        ""
        "The result will look like this:";
        Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data
    }
}

For my <Headset> device, the output looked similar to:

To query battery for <Headset>, run the following:
Get-PnpDeviceProperty -InstanceId 'BTHENUM\{0000####-0000-####-####-############}_VID&########_PID&####\#&########&#&############_#########' -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2'

The result will look like this:
90
Lockszmith
  • 2,173
  • 1
  • 29
  • 43
  • Hi, nice solution. However the output needs quotes around the instance id, e.g. Get-PnpDeviceProperty -InstanceId 'BTHENUM\{0000####-0000-####-####-############}_VID#######_PID###\###################_#########' -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' – misterjaytee Apr 07 '23 at 10:24
  • Thank @misterjaytee, fixed. Next time feel free to edit the answer yourself - as far as I understood it's not than appropriate here. – Lockszmith Apr 07 '23 at 11:45
  • I would edit, but it won't let you add less than 6 characters. You also need quotes around $($test.InstanceId) in the printed output on line 7. Again it won't let me do that edit. – misterjaytee Apr 08 '23 at 12:30
  • Got it - I've been hit with the more than 6 characters edit limit in the past. There is a way around it. you add a longer than 6 letters sentance ot the end of the edit. Something like: "EDIT: need more than 6 chracters" and submit the edit. Then you can edit it agian - and remove that EDIT note from the bottom. – Lockszmith Apr 08 '23 at 18:20
0

It seems the solution worked for few who came looking here.
It is NOT giving me the desired results. I am using Jabra's headset.
I gathered this whole set of data at its different battery level.
Unfortunately for me, I am NOT getting a KeyName who's value reflects battery percentage.
I do NOT see any other value from the list that changed as per battery percentage shown in GUI.

My set of KeyName is as follwing:

 {2BD67D8B-8BEB-48D5-87E0-6CDA3428040A} 4
 {3464F7A4-2444-40B1-980A-E0903CB6D912} 10
 {3B2CE006-5E61-4FDE-BAB8-9B8AAC9B26DF} 8
 {80497100-8C73-48B9-AAD9-CE387E19C56E} 6
 {83DA6326-97A6-4088-9453-A1923F573B29} 10
 {83DA6326-97A6-4088-9453-A1923F573B29} 15
 {83DA6326-97A6-4088-9453-A1923F573B29} 17
 {83DA6326-97A6-4088-9453-A1923F573B29} 3
 {A35996AB-11CF-4935-8B61-A6761081ECDF} 12
 {A8B865DD-2E3D-4094-AD97-E593A70C75D6} 26

Microsoft is itself reading that data, that's how they display it in notification.
Is Microsoft ready to roll out easier reading mechanism for such crucial information?

  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/34453278) – zerocukor287 May 27 '23 at 17:43