# Script to fetch battery status from Windows

It is important to watch the battery level of your work devices to ensure employee productivity. Admins can track battery health and avoid situations where a device turns off due to a low battery. Likewise, you can assess battery life and take action to improve performance. Use Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action to retrieve the battery status of your Windows devices.

 Disclaimer:The Sample Scripts provided below are adapted from third-party Open-Source sites.

 

PowerShell script to fetch battery status
-----------------------------------------

Run this script to fetch the estimated battery charge remaining on your device.

Script to fetch battery status

$BatteryStatus = @{ Name = 'BatteryStatusText' Expression = { $value = $\_.BatteryStatus switch(\[int\]$value) { 1 {'Battery Power'} 2 {'AC Power'} 3 {'Fully Charged'} 4 {'Low'} 5 {'Critical'} 6 {'Charging'} 7 {'Charging and High'} 8 {'Charging and Low'} 9 {'Charging and Critical'} 10 {'Undefined'} 11 {'Partially Charged'} default {"$value"} } } } Get-CimInstance -ClassName Win32\_Battery | Select-Object -Property DeviceID, BatteryStatus, $BatteryStatus # get battery charge: $charge = Get-CimInstance -ClassName Win32\_Battery | Select-Object -ExpandProperty EstimatedChargeRemaining "Current Charge: $charge %." 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

  $BatteryStatus = @{

 Name = 'BatteryStatusText'

 Expression = {

 $value = $\_.BatteryStatus 

 switch(\[int\]$value)

 {

 1 {'Battery Power'}

 2 {'AC Power'}

 3 {'Fully Charged'}

 4 {'Low'}

 5 {'Critical'}

 6 {'Charging'}

 7 {'Charging and High'}

 8 {'Charging and Low'}

 9 {'Charging and Critical'}

 10 {'Undefined'}

 11 {'Partially Charged'}

 default {"$value"}

 }

 }

}

Get-CimInstance -ClassName Win32\_Battery | Select-Object -Property DeviceID, BatteryStatus, $BatteryStatus

\# get battery charge: 

$charge = Get-CimInstance -ClassName Win32\_Battery | Select-Object -ExpandProperty EstimatedChargeRemaining

"Current Charge: $charge %." 

   

 

  

The command returns the current battery status with the estimated percentage charge remaining.

PowerShell script to view battery health
----------------------------------------

You can use this command to generate a battery health report of your device and save it on your device at a location you specify.

Script to fetch battery health

C: CD\\ MD Reports Powercfg /batteryreport /output c:\\reports\\Battery.html 

   1

2

3

4

5

6

7

  C:

CD\\

MD Reports 

Powercfg /batteryreport /output c:\\reports\\Battery.html 

   

 

  

The command saves a very detailed battery health report of your device on the C drive. These include:

- Battery Report: Displays system info like computer’s name, BIOS version, OS build, and the date the report was created.
- Installed batteries: Battery info such as name, manufacturer, serial number, chemistry and design capacity.
- Recent usage- detailed report of when the device was running on plugged in AC power or battery over the last three days with start time, source, state and remaining capacity.
- Battery usage- Displays battery drain over the last three days.
- Usage history-History and duration of device running on AC or battery.
- Battery capacity history-Full charge capacity vs battery design capacity of the device.
- Battery life estimates- Estimated battery life at full charge compared to designed capacity.

The report is saved as an HTML file and can be viewed in your default web browser.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.