macOS Battery Percentage Remaining

Have you ever wanted to find out what the max charge percentage is remaining on your Macintosh laptop battery? The older your battery gets, the more it’s cycled. The more it’s cycled, the more it loses its ability to reach the original maximum charge. Identifying this percentage is actually easier than I thought!

You can use ioreg to pull the following values:

  • MaxCapacity = Current max charge (due to aging)
  • DesignCapacity = Initial max charge (from factory)
ioreg -b -w 0 -f -r -c AppleSmartBattery | egrep '"(MaxCapacity|DesignCapacity)"' | grep -v '"BatteryData"' 
      "MaxCapacity" = 5821
      "DesignCapacity" = 7336

A little command line magic in the Terminal and you can calculate your battery’s available max charge as compared to when it came from the factory.

ioreg -b -w 0 -f -r -c AppleSmartBattery | egrep '"(MaxCapacity|DesignCapacity)"' | grep -v '"BatteryData"' | perl -pe 's/.*\=\s(\d+)/\1/g' | awk '{a=$1; getline; b=$1; print a/b}'
0.793484

Above we can see that my Macbook Pro’s battery is capable of charging up to 79.34% of its original maximum charge. Meaning I’ve lost 20% of my battery. Farts!

Here’s how to add a battery() function to your bash profile. Add this function and then just type battery in the Terminal to get the % of your max battery remaining!

## Adding battery() function to ~/.bash_profile
printf "\nfunction battery () {\n\tioreg -b -w 0 -f -r -c AppleSmartBattery | egrep '\"(MaxCapacity|DesignCapacity)\"' | grep -v '\"BatteryData\"' | perl -pe 's/.*\=\s(\d+)/\\\1/g' | awk '{a=\$1; getline; b=\$1; print a/b}'\n}\n" >> ~/.bash_profile

## Reviewing the 'battery' function added to ~/.bash_profile
grep -A 2 'battery ()' ~/.bash_profile

## Reload profile
source ~/.bash_profile

## Use it!!
battery
0.793484

Here’s a screenshot of things in action:

Image
The function in action.

Have fun!

  • @rj_chap

Leave a Reply