When it comes to macOS, there are multiple avenues to uninstall apps: dragging and dropping them to the trash, using a dedicated uninstaller, or going through the Launchpad. However, these methods often lack the efficiency and automation needed for large-scale operations in IT environments. For IT professionals and Managed Service Providers (MSPs) tasked with managing a fleet of Mac devices, using a shell script for this purpose becomes the most efficient and effective method.
Uninstall Apps on macOS with Shell Script
#!/usr/bin/env bash # Description: This will search under /Applications/* and under each user's Applications folder for the app you speficy to remove and will only remove the first found app. # # Release Notes: Initial Release # By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use. # Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. # Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. # Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. # Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. # Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. # Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. # EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA). # # Usage: # <ApplicationName.app> # Accepts only one application # Specify the exact name of the application. # Applications with space(s) in the name will need double quotes surrounding it. Example: "Google Chrome.app" # As macOS's file system is typically case sensitive, matching the case is important. # # EXAMPLE # If we have /Applications/Docker.app installed. # Then our argument would be: # Docker.app # EXAMPLE # If we have /Applications/Google Chrome.app installed. # Then our argument would be: # "Google Chrome.app" APP=$1 # Get a list of all installed app's, filter to only have /Applications and /User/<username>/Applications, filter the requested app, select the first found app mdfind kMDItemContentTypeTree=com.apple.application-bundle -onlyin >/dev/null APP_TO_UNINSTALL=$(system_profiler SPApplicationsDataType 2>/dev/null | sed -n 's/^ *Location: (.*)/1/p' | grep -E '^/Applications.*|/Users/.+/Applications.*' | grep "${APP}" | head -n 1) echo "Found ${APP_TO_UNINSTALL}" echo "Removing ${APP_TO_UNINSTALL}" # Remove app rm -rf "${APP_TO_UNINSTALL}" status=$? # Output result [ $status -eq 0 ] && echo "Removed ${APP_TO_UNINSTALL}" || echo "Failed to remove ${APP_TO_UNINSTALL}" # Return status exit $status
Access over 300+ scripts in the NinjaOne Dojo
The Script’s Anatomy: How it Works
Utilizing mdfind and system_profiler
The script employs macOS native utilities to accomplish its task. mdfind, a part of Spotlight, is used initially to query the metadata of files. This allows the script to compile a list of all application bundles, only focusing on those located in /Applications and /Users/<username>/Applications.
system_profiler, another macOS utility, gathers detailed information about the installed applications. When used with SPApplicationsDataType, it lists out all applications installed on the machine.
Filtering Results with grep
The grep command-line utility is used twice in the script. The first occurrence filters through the output of system_profiler to find paths that either start with /Applications or with /Users/<username>/Applications. The second usage narrows down these results to match the exact name of the app specified.
The Mighty rm
The actual uninstallation occurs with the rm -rf command, a powerful utility that removes files and directories. Here, it targets the application bundle found by the previous commands and removes it from the system.
The Perks of Automation: Why Use This Script
Time Savings
Automation is synonymous with time-saving. This script eliminates the tediousness of manual uninstallation, allowing IT personnel to focus on more critical tasks.
Scalability
The script can be a part of a more extensive automation scheme. Imagine incorporating it into a system management tool that remotely triggers this script on all Mac devices within a network. The scalability here is immense.
Accuracy
Unlike manual methods where human error is a factor, the script is programmed to find the exact app bundle, thus reducing the chances of unintentional deletions or overlooks.
Flexibility
With the ability to specify the exact name of the application as an argument, you can tailor the uninstallation process to your specific needs. Wrap this script in another script, and you’ve got a powerful tool to uninstall multiple apps sequentially.
Compliance Benefits
In industries that are regulated or have strict compliance standards, failing to remove non-compliant or unauthorized apps can lead to penalties. Automating the uninstallation process ensures that you’re always in compliance with minimal effort.
Wrapping Up
NinjaOne is a cloud-based IT management platform that can be used to automate a variety of tasks, including app uninstall. With NinjaOne, you can create a policy that runs the shell script. This policy can be scheduled to run on a recurring basis, or it can be triggered by a specific event, such as the installation of a new app.
Here are the steps on how to use NinjaOne to automate the app uninstall script:
- Create a policy in NinjaOne.
- In the policy, select the “Script” action.
- Paste the shell script into the script editor.
- In the “Arguments” section, specify the app name that you want to uninstall.
- Schedule the policy to run on a recurring basis, or trigger it by a specific event.
Once the policy is created, it will automatically uninstall the specified app from all macOS devices that are managed by NinjaOne. This can save you a significant amount of time and effort, and it can help you to ensure that all of your macOS devices are compliant with your organization’s security policies.
Here are some additional benefits of using NinjaOne to automate the app uninstall script:
- Centralized management: NinjaOne provides a centralized console where you can manage all of your macOS devices. This makes it easy to create and deploy policies, and it also makes it easier to track the status of your devices.
- Scalability: NinjaOne can be scaled to manage a large number of macOS devices. This makes it a good choice for organizations with a large IT infrastructure.
- Security: NinjaOne is a secure platform that uses industry-standard encryption to protect your data. This helps to ensure that your app uninstall scripts are secure and that your data is protected.