Category filter
Script to wipe and reinstall macOS
Apple regularly releases OS upgrades to repair bugs and improve the security of the OS. So naturally, organizations prefer that their devices run the most recent OS version. The sheer number of devices, however, makes it difficult for the IT department of any firm to deploy OS updates. So here is a script that will remotely wipe and reinstall macOS to the latest version. You can deploy it using Hexnode’s Execute Custom Script action.
Wipe and reinstall macOS
The script functions in the following manner. First, the script fetches device info such as the current OS version and processor. If the OS version is less than 10.15, a prompt is displayed “This script supports only 10.15 or higher”.
Then, the script fetches the installer of the latest version of the OS supported by the device. This means that if the device is on Big Sur Version 11.2.1 then the script installs the latest version of Big Sur (11.5).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
#!/bin/bash installerFileName=$(ls /Applications/ | grep "Install macOS") cpuNameIntel=$(system_profiler SPHardwareDataType 2>/dev/null | awk -F ": " '/Processor Name/ {print $NF}') cpuNameApple=$(system_profiler SPHardwareDataType 2>/dev/null | awk -F ": " '/Chip/ {print $NF}') macOSVersion=$(sw_vers -productVersion) macOSMajor=$(sw_vers -productVersion | cut -d '.' -f1) macOSMinor=$(sw_vers -productVersion | cut -d '.' -f2) macOSName=$(awk '/SOFTWARE LICENSE AGREEMENT FOR macOS/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'macOS ' '{print $NF}' | awk '{print substr($0, 0, length($0)-1)}') password=$1 currentUser=$2 if [ -z $currentUser ]; then currentUser=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }') fi if [ $macOSMajor -eq 10 ] && [ $macOSMinor -lt 15 ]; then echo "This script supports only 10.15 or higher" exit 0 fi if [[ $installerFileName == "" ]] ; then if [ $macOSMajor -eq 10 ] && [ $macOSMinor -eq 15 ]; then softwareupdate --fetch-full-installer --full-installer-version "10.15.7" fi installerFileName=$(ls /Applications/ | grep "Install macOS") if [ "$installerFileName" == "" ] && [ $macOSMajor -eq 10 ] ; then echo "Catalina is no longer supported" fi if [ $macOSMajor -gt 10 ] ; then macOSVersion=$(softwareupdate --list-full-installers | grep "$macOSName" | head -n 1 | awk -F'Version:' '{print $2}' | awk '{print $1;}' | tr -d ,) softwareupdate --fetch-full-installer --full-installer-version $macOSVersion installerFileName=$(ls /Applications/ | grep "Install macOS") fi fi if [ -z "$cpuNameApple" ] && [ $macOSMajor -eq 10 ]; then if [ -z "$currentUser" ]; then echo "Current user cannot be found. Exiting program." exit 1 fi "/Applications/$installerFileName/Contents/Resources/startosinstall" --eraseinstall --agreetolicense --nointeraction elif [ -n "$cpuNameApple" ] || [ $macOSMajor -gt 10 ]; then if [ -z "$password" ]; then echo "This macOS device has a Apple Processor or is on Big Sur or newer. Please rerun shell script with a admin password appended" echo "Usage: sh simpleEraseInstall.sh {admin_password} {admin_user}\n" echo "Note: You can omit {admin_user} flag if expected admin is logged in. \n" exit 1 fi if [ -z "$currentUser" ]; then echo "This macOS device has a Apple Processor or is on Big Sur or newer. Please rerun shell script with a admin password appended." echo "Usage: sh simpleEraseInstall.sh {admin_password} {admin_user}\n" echo "Note: You can omit {admin_user} flag if expected admin is logged in. \n" exit 1 fi /usr/bin/expect <<EOF set timeout -1 set password ${password} spawn {/Applications/$installerFileName/Contents/Resources/startosinstall} --eraseinstall --allowremoval --agreetolicense --nointeraction --forcequitapps --passprompt --user {$currentUser} expect "By using the agreetolicense option, you are agreeing that you have run this tool with the license only option and have read and agreed to the terms.\r\nIf you do not agree, press CTRL-C and cancel this process immediately.\r\nPassword: " send "$password\r" expect EOF else echo "Could not verify if device is a Apple Product" fi #set timeout 3600 |
The script may crash while fetching or preparing the installer and can return an error. In that case, run the following command:
1 |
softwareupdate --fetch-full-installer --full-installer-version "macOS Version” |
This ensures the installer is already available in your Applications folder when you run the script.
The script completely wipes the device and reinstalls a brand-new OS version on your macOS device.