Published on February 13th, 2023 📆 | 1704 Views ⚑
0Identifying Mac laptops and desktops from the command line by checking for a built-in battery
Every so often, it may be necessary for Mac admins to deploy a script that can apply different settings to Mac desktops and laptops. A good example may be using the pmset command to apply Energy Saver settings, where you may want to apply one set of power management settings to laptops and a different set to desktops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In the example above, the Model Identifier information from the system_profiler command is used to help identify if the Mac is a desktop or laptop. In this case, the Model Identifier information is checked to see if the model identifier contains âBookâ.
If it does, itâs a laptop. Otherwise, itâs a desktop:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
However, the latest Mac laptopsâ model identifier does not contain âBookâ. This means that this identification method should no longer be considered reliable.
Whatâs an alternative way to check? One way is to use the ioreg command to see if the Mac in question has a built-in battery or not. Laptops will have a built-in battery and desktops will not. For more details, please see below the jump.
Update â 12-29-2022: It appears my original choice of detection criteria of using built-in with the ioreg command was not universally returning no output data for desktops, as I hadnât tested against Apple Silicon Mac desktops. Apple Silicon Mac desktops share a number of characteristics with Apple Silicon laptops, including having the following command respond with the following output:
Yes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
However, there is another criteria we can search for which should provide better results, which is to use BatteryInstalled in place of built-in as criteria when running the ioreg command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On a laptop, the following output should be returned:
Yes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On a desktop, either the following output or no output should be returned:
No
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
No output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Iâve updated this blog post to now reference the BatteryInstalled criteria in place of the built-in criteria previously referenced.
Note: There is an edge case where itâs possible a laptop may have its built-in battery removed and thus respond with No or return no output, like a desktop would. Mac laptops made since 2009 and later have non-removable batteries built into the laptop, so this scenario shouldnât be a common one for most environments, but itâs worth mentioning if you have a non-zero number of Mac laptops with their built-in batteries removed.
You can use the ioreg command shown below to query the information for the AppleSmartBattery hardware driver (currently used by macOS for its battery management) and check whether the Mac has a built-in battery or not:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On a laptop, the following command should return Yes:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On a desktop, the same command should return No or no output:
No
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
No output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You should be able to use this command to update the example script for setting power management to correctly identify laptops vs. desktops again:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Note: The AppleSmartBattery hardware driver is being queried by the ioreg command to gather this information. If Apple ever changes the name or the functionality of the AppleSmartBattery hardware driver, this method may stop working and need to be updated for the new name or functionality.
Gloss