Identifying 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
#!/bin/bash | |
# Set separate power management settings for desktops and laptops | |
# If it's a laptop, the power management settings for "Battery" are set to have the computer sleep in 15 minutes, | |
# disk will spin down in 10 minutes, the display will sleep in 5 minutes and the display itself will dim to | |
# half-brightness before sleeping. While plugged into the AC adapter, the power management settings for "Charger" | |
# are set to have the computer never sleep, the disk doesn't spin down, the display sleeps after 30 minutes and | |
# the display dims before sleeping. | |
# | |
# If it's not a laptop (i.e. a desktop), the power management settings are set to have the computer never sleep, | |
# the disk doesn't spin down, the display sleeps after 30 minutes and the display dims before sleeping. | |
# | |
# Detects if this Mac is a laptop or not by checking the model ID for the word "Book" in the name. | |
IS_LAPTOP=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book") | |
if [[ -n "$IS_LAPTOP" ]]; then | |
/usr/bin/pmset -b sleep 15 disksleep 10 displaysleep 5 halfdim 1 | |
/usr/bin/pmset -c sleep 0 disksleep 0 displaysleep 30 halfdim 1 | |
else | |
/usr/bin/pmset sleep 0 disksleep 0 displaysleep 30 halfdim 1 | |
fi |
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
/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book" |
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
username@computername ~ % /usr/sbin/ioreg -c AppleSmartBattery -r | awk '/built-in/ {print $3}' | |
Yes | |
username@computername ~ % |
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
/usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' |
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
username@computername ~ % /usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' | |
Yes | |
username@computername ~ % |
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
username@computername ~ % /usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' | |
No | |
username@computername ~ % |
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
username@computername ~ % /usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' | |
username@computername ~ % |
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
/usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' |
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
username@computername ~ % /usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' | |
Yes | |
username@computername ~ % |
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
username@computername ~ % /usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' | |
No | |
username@computername ~ % |
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
username@computername ~ % /usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}' | |
username@computername ~ % |
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
#!/bin/bash | |
# Set separate power management settings for desktops and laptops | |
# If it's a laptop, the power management settings for "Battery" are set to have the computer sleep in 15 minutes, | |
# disk will spin down in 10 minutes, the display will sleep in 5 minutes and the display itself will dim to | |
# half-brightness before sleeping. While plugged into the AC adapter, the power management settings for "Charger" | |
# are set to have the computer never sleep, the disk doesn't spin down, the display sleeps after 30 minutes and | |
# the display dims before sleeping. | |
# | |
# If it's not a laptop (i.e. a desktop), the power management settings are set to have the computer never sleep, | |
# the disk doesn't spin down, the display sleeps after 30 minutes and the display dims before sleeping. | |
# | |
# Detects if this Mac is a laptop or not by checking for a built-in battery. | |
IS_LAPTOP=$(/usr/sbin/ioreg -c AppleSmartBattery -r | awk '/BatteryInstalled/ {print $3}') | |
if [[ "$IS_LAPTOP" = "Yes" ]]; then | |
/usr/bin/pmset -b sleep 15 disksleep 10 displaysleep 5 halfdim 1 | |
/usr/bin/pmset -c sleep 0 disksleep 0 displaysleep 30 halfdim 1 | |
else | |
/usr/bin/pmset sleep 0 disksleep 0 displaysleep 30 halfdim 1 | |
fi |
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.
FYI I have Mac minis (M1 2020/Macmini9,1) and Mac Studios (Mac13,1) that are returning a ‘Yes’ from the ioreg command via Jamf extension attribute. I don’t have access to them at the moment to test more in-depth to see why, unless they too have some sort of internal battery that I’m not aware of.
…and a couple iMacs, as more machines are checking in:
iMac21,1
iMac21,2
Here’s the ioreg output from Macmini9,1. Interestingly, despite all the battery-related info, “BatteryInstalled” has a value of No (whereas on a MacBookPro18,3 the value is Yes).
+-o AppleSmartBattery
{
“Amperage” = 0
“CurrentCapacity” = 0
“MaxCapacity” = 0
“IOReportLegendPublic” = Yes
“BatteryInvalidWakeSeconds” = 30
“built-in” = Yes
“FullPathUpdated” = 1672179572
“AdapterInfo” = 0
“ExternalChargeCapable” = Yes
“PostChargeWaitSeconds” = 120
“Location” = 0
“IsCharging” = No
“BootPathUpdated” = 1671800289
“AdapterDetails” = {“FamilyCode”=0}
“PostDischargeWaitSeconds” = 120
“UpdateTime” = 1672179572
“AppleRawAdapterDetails” = ()
“BatteryInstalled” = No
“Voltage” = 0
“CycleCount” = 0
“IOGeneralInterest” = “IOCommand is not serializable”
“IOReportLegend” = ({“IOReportChannels”=((7167869599145487988,6460407809,”BatteryCycleCount”)),”IOReportGroupName”=”Battery”,”IOReportChannelInfo”={“IOReportChannelUnit”=0}} )
“ExternalConnected” = Yes
“TimeRemaining” = 0
}
Interesting. Is “BatteryInstalled” consistently reporting No on the outlier Mac models?
The reason I’m asking is that it may be possible to swap “BatteryInstalled” for “built-in” in the ioreg command.
iMac21,1 and the Mac Studio (Mac13,1) are both showing No for their “BatteryInstalled” values. The others haven’t yet reported in since I changed my extension attribute to include a case statement for those specific models to get more data, but I can let you know when they do. The pattern so far suggests that they will.
I can confirm that all outliers that I’ve found so far have a “BatteryInstalled” value of No, and that modifying the piped awk command accordingly does seem to produce more consistent results in separating desktop vs. laptop devices.
If you want the full ioreg output for each model type, let me know.
Yes, please. Thank you for offering to share that.
Here is the complete output of $(/usr/sbin/ioreg -c AppleSmartBattery -r) for each of those models.
iMac21,1:
+-o AppleSmartBattery
{
“Amperage” = 0
“CurrentCapacity” = 0
“MaxCapacity” = 0
“IOReportLegendPublic” = Yes
“BatteryInvalidWakeSeconds” = 30
“built-in” = Yes
“FullPathUpdated” = 1672191220
“AdapterInfo” = 0
“ExternalChargeCapable” = Yes
“PostChargeWaitSeconds” = 120
“Location” = 0
“IsCharging” = No
“BootPathUpdated” = 1670879494
“AdapterDetails” = {“SerialString”=”C4H207502EX02KNBA”,”Watts”=144,”Model”=”0x7015″,”Name”=”143W Power Adapter”,”Current”=9100,”FamilyCode”=18446744073172697098,”FwVersion”=”01060061″,”AdapterVoltage”=15800,”AdapterID”=28693,”IsWireless”=No,”HwVersion”=”00000000″,”Manufacturer”=”Apple Inc.”}
“PostDischargeWaitSeconds” = 120
“UpdateTime” = 1672191220
“AppleRawAdapterDetails” = ({“SerialString”=”C4H207502EX02KNBA”,”Watts”=144,”Model”=”0x7015″,”Name”=”143W Power Adapter”,”Current”=9100,”FamilyCode”=18446744073172697098,”FwVersion”=”01060061″,”AdapterVoltage”=15800,”AdapterID”=28693,”IsWireless”=No,”HwVersion”=”00000000″,”Manufacturer”=”Apple Inc.”})
“PowerTelemetryData” = {“WallEnergyEstimate”=2189,”AccumulatedSystemPowerIn”=6541337382,”AdapterEfficiencyLossAccumulatorCount”=1304490,”AccumulatedWallEnergyEstimate”=2107101751,”SystemPowerInAccumulatorCount”=1304490,”SystemEnergyConsumed”=1887,”SystemPowerIn”=6794,”SystemLoad”=7421,”AccumulatedSystemLoad”=7028205312,”SystemLoadAccumulatorCount”=1304491,”AccumulatedSystemEnergyConsumed”=1816422489,”AdapterEfficiencyLoss”=302,”AccumulatedAdapterEfficiencyLoss”=290679262}
“BestAdapterIndex” = 4
“BatteryInstalled” = No
“Voltage” = 0
“CycleCount” = 0
“IOGeneralInterest” = “IOCommand is not serializable”
“IOReportLegend” = ({“IOReportChannels”=((7167869599145487988,6460407809,”BatteryCycleCount”)),”IOReportGroupName”=”Battery”,”IOReportChannelInfo”={“IOReportChannelUnit”=0}} )
“ExternalConnected” = Yes
“TimeRemaining” = 0
}
iMac21,2:
+-o AppleSmartBattery
{
“Amperage” = 0
“CurrentCapacity” = 0
“MaxCapacity” = 0
“IOReportLegendPublic” = Yes
“BatteryInvalidWakeSeconds” = 30
“built-in” = Yes
“FullPathUpdated” = 1672191118
“AdapterInfo” = 0
“ExternalChargeCapable” = Yes
“PostChargeWaitSeconds” = 120
“Location” = 0
“IsCharging” = No
“BootPathUpdated” = 1668151390
“AdapterDetails” = {“SerialString”=”C4H151101XR13VVBD”,”Watts”=144,”Model”=”0x7018″,”Name”=”143W Power Adapter”,”Current”=9100,”FamilyCode”=18446744073172697098,”FwVersion”=”01060061″,”AdapterVoltage”=15800,”AdapterID”=28696,”IsWireless”=No,”HwVersion”=”00000000″,”Manufacturer”=”Apple Inc.”}
“PostDischargeWaitSeconds” = 120
“UpdateTime” = 1672191118
“AppleRawAdapterDetails” = ({“SerialString”=”C4H151101XR13VVBD”,”Watts”=144,”Model”=”0x7018″,”Name”=”143W Power Adapter”,”Current”=9100,”FamilyCode”=18446744073172697098,”FwVersion”=”01060061″,”AdapterVoltage”=15800,”AdapterID”=28696,”IsWireless”=No,”HwVersion”=”00000000″,”Manufacturer”=”Apple Inc.”})
“PowerTelemetryData” = {“WallEnergyEstimate”=1333,”AccumulatedSystemPowerIn”=20523749467,”AdapterEfficiencyLossAccumulatorCount”=4027500,”AccumulatedWallEnergyEstimate”=6611176466,”SystemPowerInAccumulatorCount”=4027500,”SystemEnergyConsumed”=1149,”SystemPowerIn”=4138,”SystemLoad”=4163,”AccumulatedSystemLoad”=20743949828,”SystemLoadAccumulatorCount”=4027501,”AccumulatedSystemEnergyConsumed”=5699127083,”AdapterEfficiencyLoss”=184,”AccumulatedAdapterEfficiencyLoss”=912049383}
“BestAdapterIndex” = 2
“BatteryInstalled” = No
“Voltage” = 0
“CycleCount” = 0
“IOGeneralInterest” = “IOCommand is not serializable”
“IOReportLegend” = ({“IOReportChannels”=((7167869599145487988,6460407809,”BatteryCycleCount”)),”IOReportGroupName”=”Battery”,”IOReportChannelInfo”={“IOReportChannelUnit”=0}} )
“ExternalConnected” = Yes
“TimeRemaining” = 0
}
Macmini9,1:
+-o AppleSmartBattery
{
“Amperage” = 0
“CurrentCapacity” = 0
“MaxCapacity” = 0
“IOReportLegendPublic” = Yes
“BatteryInvalidWakeSeconds” = 30
“built-in” = Yes
“FullPathUpdated” = 1672191612
“AdapterInfo” = 0
“ExternalChargeCapable” = Yes
“PostChargeWaitSeconds” = 120
“Location” = 0
“IsCharging” = No
“BootPathUpdated” = 1671800289
“AdapterDetails” = {“FamilyCode”=0}
“PostDischargeWaitSeconds” = 120
“UpdateTime” = 1672191612
“AppleRawAdapterDetails” = ()
“BatteryInstalled” = No
“Voltage” = 0
“CycleCount” = 0
“IOGeneralInterest” = “IOCommand is not serializable”
“IOReportLegend” = ({“IOReportChannels”=((7167869599145487988,6460407809,”BatteryCycleCount”)),”IOReportGroupName”=”Battery”,”IOReportChannelInfo”={“IOReportChannelUnit”=0}} )
“ExternalConnected” = Yes
“TimeRemaining” = 0
}
Mac13,1:
+-o AppleSmartBattery
{
“Amperage” = 0
“CurrentCapacity” = 0
“MaxCapacity” = 0
“IOReportLegendPublic” = Yes
“BatteryInvalidWakeSeconds” = 30
“built-in” = Yes
“FullPathUpdated” = 1672191735
“AdapterInfo” = 0
“ExternalChargeCapable” = Yes
“PostChargeWaitSeconds” = 120
“Location” = 0
“IsCharging” = No
“BootPathUpdated” = 1667938057
“AdapterDetails” = {“FamilyCode”=0}
“PostDischargeWaitSeconds” = 120
“UpdateTime” = 1672191735
“AppleRawAdapterDetails” = ()
“BatteryInstalled” = No
“Voltage” = 0
“CycleCount” = 0
“IOGeneralInterest” = “IOCommand is not serializable”
“IOReportLegend” = ({“IOReportChannels”=((7167869599145487988,6460407809,”BatteryCycleCount”)),”IOReportGroupName”=”Battery”,”IOReportChannelInfo”={“IOReportChannelUnit”=0}} )
“ExternalConnected” = Yes
“TimeRemaining” = 0
}
I’ve seen this behavior of the “AppleSmartBattery” being filled out with “BatteryInstalled = No” on Apple Silicon desktops as well. I believe this is because the same exact Apple Silicon SoCs are used across all Macs and they all technically support batteries while different chipset capabilities were exposed desktop vs laptop on Intel Macs.
Since checking for “BatteryInstalled = No” could technically give a rare but possible false negative for a laptop with it’s battery disconnected or removed, I think the most reliable alternative to using the Model ID is to use the “Model Name” field from “system_profiler” which will still always list “MacBook Pro” even for MacBook Pro’s with “MacXX,Y” Model IDs, and of course it can be used to check for Intel Macs too, it’s a consistent and reliable check with no edge cases like checking for a battery has.
This could be checked across all models as simply as just running “system_profiler SPHardwareDataType | grep MacBook” because that command would just get 2 matched lines on older Macs laptops with “MacBookProXX,Y” Model IDs and only one matched line on newer Macs laptops with “MacXX,Y” Model IDs and no lines would ever be matched for any desktops.
Going back to your original method of using system_profiler could you not just grep on “Model Name” instead of “Model Identifier”?