Category filter
Script to View Battery Status on macOS devices
Most Mac users rely on the battery percentage indicator or preferences to view the remaining battery life. As an administrator, if you want to monitor and determine significant changes in the state of your battery without involving direct user interaction, the Execute Custom Script action is here to help you. For instance, to avoid situations where a device gets switched off due to insufficient battery, you can remotely view the battery status and take pre-emptive measures. Retrieved information can also be used to assess your battery life and adopt measures to improve the overall performance.
Scripting Language – Bash
File extension – .sh
View Battery Status
To check the battery information, including percentage, time remaining, battery source, and battery charge status, use the single-line command below.
1 2 |
#!/bin/sh pmset -g batt |
pmset
— used to manipulate power management settings.
All the changes made through pmset are saved in a persistent preferences file at /Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist
−g ps
/ batt
displays status of batteries and UPSs.
Once you execute the script, you will receive outputs that look like any of the following:
Case 1: If the device is powered by MacBook battery
Now drawing from ‘Battery Power’
-InternalBattery-0 68%; discharging; 12:39 remaining present: true
Case 2: If the MacBook is actively charging
Now drawing from ‘AC Power’
-InternalBattery-0 93%; charging; 0:11 remaining present: true
Fetch Battery Health
To get information on the battery health in addition to the battery charge, use the command below.
1 2 |
#!/bin/sh system_profiler SPPowerDataType | sed -n '/Charge Information:/,/System Power Settings:/p' | sed '$d' |
system_profiler
– Reports system hardware and software configuration
SPPowerDataType
– Gets information on Battery
Once the script is executed successfully, you can view a similar output as the following from the Action History tab:
Charge Information:
The battery’s charge is below the warning level – Displays’ Yes’ if the battery’s charge is below 10%. For higher battery charge, ‘No’ is shown.
Fully Charged – Displays’ Yes’ if the battery is fully charged (100%). Otherwise, ‘No’ is shown.
Charging – Displays’ Yes’ if the battery is being charged. Otherwise, ‘No’ is shown.
State of Charge (%) – Displays the current charge of the battery in percentage.
Health Information:
Cycle Count – Displays the number of times the battery has gone through charge cycles. A charge cycle occurs when you charge your battery to its maximum capacity and then use it entirely until it’s 0%.
Condition – Displays ‘Normal’ if the battery is functioning normally. Alternatively, it shows ‘Service recommended’ if there might be a need to replace the battery due to its reduced ability to hold charge than when it was new.
Maximum Capacity – Indicates the battery capacity relative to when it was 100%. Lower capacity indicates lesser hours of usage between charges.