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

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