Already a NinjaOne customer? Log in to view more guides and the latest updates.

Custom Scripts: Remove TeamViewer (macOS)

Topic

This article explains how to use a custom script to remove TeamViewer from your macOS endpoints.

For Microsoft Windows endpoints, refer to our Custom Scripts: Remove TeamViewer (Windows) article.

Environment

NinjaOne Endpoint Management

Description

If you need to remove instances of TeamViewer from your endpoints, the Remove TeamViewer script can make the process easier.

You can either import it from our Template Library or upload the version attached to this article to your Automation Library in Bash format. Then, you can run it on devices by using a Scheduled Automation, a Scheduled Task, or ad hoc from the play button on the device page or the Devices tab.

Script

#!/usr/bin/env bash
#
# Description: Uninstalls TeamViewer.
#
# Example: (No Parameters)
#
# Verifying that TeamViewer is still installed.
# Removing TeamViewer using '"/Library/Application Support/TeamViewer/TeamViewerUninstaller.app/Contents/Helpers/UninstallTeamViewer" --force'
# Uninstalling...
# Done.
# Verifying that TeamViewer has been removed.
# TeamViewer has been successfully removed.
#
# Version: 1.0
# Release Notes: Initial Release

# Function to wait for the TeamViewer process to stop, with a timeout of 5 minutes.
waitForTeamViewer() {
  # Set timeout to 5 minutes from now.
  timeout=$(date -v+5M "+%s")

  # Check if the TeamViewer process is running.
  if pgrep -ifl "TeamViewer" 1>/dev/null; then
    echo "Waiting for the TeamViewer process to stop."
  fi

  # Loop until the TeamViewer process stops or the timeout is reached.
  while pgrep -ifl "TeamViewer" 1>/dev/null; do
    if [[ $(date "+%s") -ge "$timeout" ]]; then
      echo "[Warning] The five minute timeout has been reached."
      break
    fi

    # Wait briefly before checking again.
    sleep 0.1
  done
}

# Function to print an error message and exit with a specified return code.
die() {
  local _ret="${2:-1}"
  echo "$1" >&2
  exit "${_ret}"
}

echo " "

# Initialize the exit code variable if not already set.
if [[ -z "$exitCode" ]]; then
  exitCode=0
fi

# Ensure the script is run with root permissions.
if [[ $(id -u) -ne 0 ]]; then
  _PRINT_HELP=no die "[Error] This script must be run with root permissions. Try running it with sudo or as the system/root user." 1
fi

echo "Verifying that TeamViewer is still installed."

# Check if the TeamViewer uninstaller is available using `mdfind`.
if mdfindResult=$(mdfind kind:application "TeamViewer" 2>/dev/null | grep -v '^$'); then
  teamViewerUninstaller=$(echo "$mdfindResult" | grep "TeamViewerUninstaller")
  teamViewerInstalled="true"
fi

# Check if the TeamViewer application directory exists.
if [[ -d "/Applications/TeamViewer.app" ]]; then
  teamViewerInstalled="true"
fi

# Check if the specific uninstaller path still exists; if so, exit with an error.
if [[ -f "/Library/Application Support/TeamViewer/TeamViewerUninstaller.app/Contents/Helpers/UninstallTeamViewer" ]]; then
  teamViewerUninstaller="/Library/Application Support/TeamViewer/TeamViewerUninstaller.app"
  teamViewerInstalled="true"
fi

# If TeamViewer is not installed, check for the QuickSupport tool and exit with an error.
if [[ "$teamViewerInstalled" != "true" ]]; then
  if pgrep -ifl "TeamViewer" 1>/dev/null; then
    echo "[Alert] The TeamViewer QuickSupport tool was detected. The QuickSupport tool is not installed on the system because it is a portable app."
  fi
  _PRINT_HELP=no die "[Error] TeamViewer is not installed on this system." 1
fi

# Attempt to uninstall TeamViewer using the uninstaller(s) found by `mdfind`.
if [[ -n "$teamViewerUninstaller" ]]; then
  while IFS= read -r uninstaller; do
    echo "Removing TeamViewer using '"$uninstaller/Contents/Helpers/UninstallTeamViewer" --force'"

    # Run the uninstaller with the `--force` option.
    if ! "$uninstaller/Contents/Helpers/UninstallTeamViewer" --force; then
      _PRINT_HELP=no die "[Error] Failed to uninstall TeamViewer." 1
    fi

    # Wait for the TeamViewer process to stop.
    waitForTeamViewer

    # Check if the uninstaller is still present; if not, break the loop.
    if ! mdfind kind:application "TeamViewer" 2>/dev/null | grep "TeamViewerUninstaller" 1>/dev/null; then
      break
    fi
  done <<< "$teamViewerUninstaller"

  uninstallAttempted="true"
fi

# Attempt to uninstall TeamViewer using a specific uninstaller path.
if [[ -f "/Library/Application Support/TeamViewer/TeamViewerUninstaller.app/Contents/Helpers/UninstallTeamViewer" ]]; then
  echo "Removing TeamViewer using '"/Library/Application Support/TeamViewer/TeamViewerUninstaller.app/Contents/Helpers/UninstallTeamViewer" --force'"

  # Run the uninstaller with the `--force` option.
  if ! "/Library/Application Support/TeamViewer/TeamViewerUninstaller.app/Contents/Helpers/UninstallTeamViewer" --force; then
    _PRINT_HELP=no die "[Error] Failed to uninstall TeamViewer." 1
  fi

  # Wait for the TeamViewer process to stop.
  waitForTeamViewer

  uninstallAttempted="true"
fi

# If no uninstallation attempt was made, exit with an error.
if [[ "$uninstallAttempted" != "true" ]]; then
  _PRINT_HELP=no die "[Error] Failed to find the TeamViewer uninstaller." 1
fi

echo "Verifying that TeamViewer has been removed."

# Check if the app is still present; if so, exit with an error.
if foundApplications=$(mdfind kind:application "TeamViewer" 2>/dev/null); then
  while IFS= read -r application; do
    if [[ -f "$application" || -d "$application" ]]; then
      _PRINT_HELP=no die "[Error] Failed to uninstall TeamViewer." 1
    fi
  done <<< "$foundApplications"
fi

# Check if the app is still present; if so, exit with an error.
if [[ -d "/Applications/TeamViewer.app" ]]; then
  _PRINT_HELP=no die "[Error] Failed to uninstall TeamViewer." 1
fi

# Check if the specific uninstaller path still exists; if so, exit with an error.
if [[ -f "/Library/Application Support/TeamViewer/TeamViewerUninstaller.app/Contents/Helpers/UninstallTeamViewer" ]]; then
  _PRINT_HELP=no die "[Error] Failed to uninstall TeamViewer." 1
fi

echo "TeamViewer has been successfully removed."

# Exit the script with the specified exit code.
exit "$exitCode"

 

FAQ

Next Steps