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

Disclaimer:

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

View Battery Status

To check the battery information, including percentage, time remaining, battery source, and battery charge status, use the single-line command below.

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.

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:

Fetch Battery charge and battery health information

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.

Notes:

  • If the Mac doesn’t have an internal battery, as is the case with iMacs, no useful information will be reported.
  • 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.

  • Sample Script Repository