# Script to set charging limit on macOS devices

Charging limits are set on macOS devices to prolong their battery health; this is especially beneficial for Macs that are always plugged in as it prevents the battery from constantly being fully charged. When devices are charged to maximum capacity for an extended period, the battery loses its capacity, gradually decreasing its life. When a charging limit is set, the battery will not charge beyond that value, even if it continues to be connected to a power source. Admins may need to employ this battery charging limit in many devices, which could be a hefty task if done manually. This process can be made much more efficient by remotely executing the scripts given below using Hexnode UEM’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action.

Scripting Language – Bash

File extension – .sh

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

How to set charging limit on Macs?
----------------------------------

First, install **Homebrew** if it isn’t already available on the device. Homebrew is a software package management system that simplifies software installation in macOS. Execute the following command to install Homebrew:

Script to install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

   1

  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

   

 

  

Once brew is installed, run these commands to install bclm:

Script to install bclm

brew tap zackelia/formulae; brew install bclm

   1

2

  brew tap zackelia/formulae;

brew install bclm

   

 

  

**bclm** represents Battery Charge Limit Max, a wrapper to read and write values to the System Management Controller (SMC). SMC controls how the Mac manages its power.

When executing these commands, if a “command not found” error arises, provide the entire path to brew, which can be obtained by running:

Script to get location of brew

which brew

   1

  which brew

   

 

  

For further assistance, check out our document on how to [fix common issues](https://www.hexnode.com/mobile-device-management/help/fix-common-issues-while-executing-custom-scripts-on-mac/) while executing custom scripts on Mac.

After the installation of bclm, run the `write` command to set charging limit.

Script to get location of brew

sudo bclm write 77

   1

  sudo bclm write 77

   

 

  

Executing this command will set the charging limit to 77%.

How to make sure the charging limit persists?
---------------------------------------------

The charging limit is set by changing the SMC’s value. To ensure that the value persists and doesn’t reset, execute the following command:

Script to get location of brew

sudo bclm persist

   1

  sudo bclm persist

   

 

  

Execute this command to undo the persist action:

Script to get location of brew

sudo bclm unpersist

   1

  sudo bclm unpersist

   

 

  

How to view the existing charging limit?
----------------------------------------

Script to get location of brew

bclm read

   1

  bclm read

   

 

  

Execute this command to view the battery’s current charging limit in percentage.

The output can be verified under the **Action History** tab of the device. Since we set the charging limit to 77% using the `write` command earlier, the output here will be:

![Output of script to view set battery charge limit](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2022/10/View-existing-charging-limit.png "View existing charging limit")[](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2022/10/View-existing-charging-limit.png)

 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.