# 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 ](https://www.hexnode.com/mobile-device-management/help/how-enforce-os-updates-on-macos-devices-using-hexnode/)action, [OS update](https://www.hexnode.com/mobile-device-management/help/how-to-schedule-macos-updates-using-hexnode-mdm/) via policies and by [deploying scripts](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/). This document contains the code snippet required to update and upgrade macOS devices.

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

 

Script to Update OS
-------------------

To simply list available updates for the current OS version of your device, use the command-

Script to list all available updates on Mac

softwareupdate -l 

   1

  softwareupdate -l 

   

 

  

To install all available updates listed with the above command and restart the system to complete installation, use the following command

Script to update OS after restart in macOS devices

sudo softwareupdate -i -a -R

   1

  sudo softwareupdate -i -a -R

   

 

  

To avoid running two commands separately, you can use a combination of these commands.

Script to perform software update on Mac

\#!/bin/bash getosupd=$(softwareupdate -l | grep "Big Sur" | awk NR==1 | cut -d ' ' -f 3-) softwareupdate -i "$getosupd" -R

   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
--------------------

Script to perform software upgrade on Mac

\#!/bin/bash osVersion=<OS Version> installerPath="" majorVersion=$(echo $osVersion | cut -d "." -f 1) minorVersion=$(echo $osVersion | cut -d "." -f 2) if \[ $majorVersion == "12" \];then installerPath="install macOS Monterey.app" elif \[ $majorVersion == "11" \];then installerPath="Install macOS Big Sur.app" elif \[ $minorVersion == "15"\* \];then installerPath="Install macOS Catalina.app" fi fullPath="/Applications/$installerPath/Contents/Resources/startosinstall" softwareupdate --fetch-full-installer –full-installer-version $osVersion echo <Password> | "$fullPath" --agreetolicense --forcequitapps --nointeraction --user <Username> --stdinpass

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

  \#!/bin/bash

osVersion=<OS Version>

installerPath=""

majorVersion=$(echo $osVersion | cut -d "." -f 1)

minorVersion=$(echo $osVersion | cut -d "." -f 2)

if \[ $majorVersion == "12" \];then

 installerPath="install macOS Monterey.app"

elif \[ $majorVersion == "11" \];then

 installerPath="Install macOS Big Sur.app"

elif \[ $minorVersion == "15"\* \];then

 installerPath="Install macOS Catalina.app"

fi

fullPath="/Applications/$installerPath/Contents/Resources/startosinstall"

softwareupdate --fetch-full-installer –full-installer-version $osVersion

echo <Password> | "$fullPath" --agreetolicense --forcequitapps --nointeraction --user <Username> --stdinpass

   

 

  

Here, <OS Version> is to be replaced with the required OS version, i.e., the version to which the OS should be updated. Likewise, <Username> and <Password> are to be replaced with the username and password of the admin, respectively.

This script receives the required OS version as input, installs the corresponding version’s installer app and initiates the update installation. For example, if the admin enters 12. 1 as the required OS version, the macOS Monterey installer app will be installed, and once the admin credentials are given, the update will begin to install.

### Here is an alternative step-by-step guidance to upgrade the OS:

To list all available OS versions for installation, use the following command –

Script to list all available OS versions for installation on Mac

\#!/bin/bash softwareupdate --list-full-installers | grep 'macOS' | awk '{print ++count " " $0}' 

   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 and check if installers are available for the latest OS version for your device.

Script to list all installers on Mac

softwareupdate --list-full-installers 

   1

  softwareupdate --list-full-installers 

   

 

  

Fetch the installer app to your device using the command:

Script to check if installers are available for the latest OS version on Mac

softwareupdate --fetch-full-installer 

   1

  softwareupdate --fetch-full-installer 

   

 

  

Using this command, you can fetch a specific OS version of the installer app from the list that had been displayed earlier, for instance, version 12.1:

Sample script

softwareupdate --fetch-full-installer --full-installer-version 12.1 

   1

  softwareupdate --fetch-full-installer --full-installer-version 12.1 

   

 

  

After running the command, the corresponding installer app should be available in your Applications folder. Here, since **version 12.1** was chosen, **Install macOS Monterey.app** will be present.
![Get the installer app from the App Store ](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2022/08/Installer-app-should-be-available-in-the-Applications-folder.jpeg)

If the OS installer app is available in the Application folder of your device, you can initiate installation using the command

Sample script

'/Applications/Install macOS Monterey.app/Contents/Resources/startosinstall' --agreetolicense --nointeraction --forcequitapps 

   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.

If your device has the new M1 chip, `starttoinstall` requires the admin username and password to initiate installation.

Sample script

echo <Password> |'/Applications/Install macOS Monterey.app/Contents/Resources/startosinstall' --agreetolicense --nointeraction --forcequitapps --user <adminuser> --stdinpass 

   1

  echo <Password> |'/Applications/Install macOS Monterey.app/Contents/Resources/startosinstall' --agreetolicense --nointeraction --forcequitapps --user <adminuser> --stdinpass 

   

 

  

`--user `– admin user to authorize installation

`--stdin` – to pass the password without interaction

Pass the device admin credentials to the device by replacing <adminuser> and <Password> with the admin’s username and password respectively, and execute the upgrade action without any user interaction.

 Notes:- Ensure your device is plugged in and has sufficient space to download updates before initiating OS update.
- Ensure your device is eligible for updates.
- Scripts may be in ‘Initiated’ status for an extended period of time depending on network connectivity as a large number of updates are downloaded.
- 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.