Category filter
Script to Update and Upgrade OS in macOS devices
Apple releases OS updates periodically to fix bugs or strengthen the security of the released versions of the OS. Organizations prefer their devices to have the latest available OS. However, deploying OS updates can be a cumbersome task for the IT department of any organization because of the sheer number of devices. Hexnode provides three methods to update macOS devices; Update OS action, OS update via policies and by deploying scripts. This document contains the code snippet required to update and upgrade macOS devices.
Script to Update OS
To simply list available updates for the current OS version of your device, use the command-
1 |
softwareupdate -l |
To install all available updates listed with the above command and restart the system when installation is complete, use the following command
1 |
softwareupdate -i –a -restart |
To avoid running two commands separately, you can use a combination of these commands.
1 2 3 |
#!/bin/bash getosupd=$(softwareupdate -l | grep "Big Sur" | awk NR==1 | cut -d ' ' -f 3-) softwareupdate -i "$getosupd" -R |
The above code uses a combination of commands and tools to update the OS to the latest version of macOS Big Sur available.
softwareupdate –l
command is used to fetch the list of all available software updates.
grep “Big Sur”
scans the list for available versions of macOS Big Sur. You may replace “Big Sur” in the above code with an OS version name suitable for your use case.
awk NR==1
filters the updated list to the latest version (row number 1).
cut –d ‘ ‘ -f 3-
further processes the output to contain only the OS name identifier.
Finally, we pass the OS name identifier as an argument $getosupd
with the softwareupdate
command to install the specified OS version. The -i
command installs the OS and the -R
command can be added to automatically restart the system when the OS installation is complete.
Script to Upgrade OS
To list all available OS versions for installation, use the following command –
1 2 |
#!/bin/bash softwareupdate --list-full-installers | grep 'macOS' | awk '{print ++count " " $0}' |
The --list-full-installers
command lists all available macOS installers. This command may not work as intended on versions of macOS older than Catalina.
You could also just run the command to list all installers
1 |
softwareupdate --list-full-installers |
Check if installers are available for the latest OS version for your device.
Fetch the installer app to your device using the command:
1 |
softwareupdate --fetch-full-installer |
You could choose a specific OS version of the installer app to fetch by using the command:
1 |
softwareupdate --fetch-full-installer --full-installer-version 12.1 |
After running the command, Install macOS Monterey.app should be available in your Applications folder.
If the OS installer app is available in the Application folder of your device, you can initiate installation using the command
1 |
'/Applications/Install macOS Monterey.app/Contents/Resources/startosinstall' --agreetolicense –nointeraction --forcequitapps |
The update will be installed without any interaction from the user and the system will be rebooted.