Back to Resources
Level
Script
Security
This script streamlines the process of removing or disabling unauthorized, stale, or compromised macOS user accounts by automating tasks that would otherwise require manual attention, ensuring your organization maintains secure and compliant endpoints with minimal effort.
It uses a comma-separated list of usernames from the “UsersToDelete” variable and iterates through each entry, checking for existing accounts before disabling login shells, removing admin privileges, and locking the account. By default, it only disables users, but you can uncomment a line to delete them entirely. The script seamlessly integrates with Level’s root-level permissions, making manual elevation steps unnecessary and ensuring a consistent approach to user account management across all your Mac devices.
1#!/bin/bash
2
3# This resource is provided as a convenience for Level users. We cannot
4# guarantee it will work in all environments. Please test before deploying
5# to your production environment. We welcome contributions to our community
6# library
7
8# Level Library
9# https://level.io/library/script-macos-delete-disable-users
10
11# Define the list of users to disable/delete (replace with actual values)
12USERS_TO_DELETE="{{UsersToDelete}}"
13
14# Convert the comma-separated list into an array
15IFS=',' read -r -a userArray <<< "$(echo "$USERS_TO_DELETE" | sed 's/, */,/g')"
16
17for user in "${userArray[@]}"; do
18 # Trim spaces from the username
19 user=$(echo "$user" | xargs)
20
21 # Check if user exists
22 if dscl . -list /Users | grep -q "^$user$"; then
23 echo "User $user exists. Proceeding with disabling..."
24
25 # Remove Secure Token if enabled
26 if sudo sysadminctl -secureTokenStatus "$user" 2>&1 | grep -q "ENABLED"; then
27 echo "User $user has Secure Token. Attempting to disable..."
28 sudo sysadminctl -secureTokenOff -username "$user" -password "<current_password>"
29 fi
30
31 # Remove user from admin group
32 if dseditgroup -o checkmember -m "$user" admin | grep -q "is a member"; then
33 sudo dseditgroup -o edit -d "$user" -t user admin
34 echo "User $user has been removed from the admin group."
35 fi
36
37 # Disable login shell
38 sudo dscl . -create /Users/$user UserShell /usr/bin/false
39 echo "User $user's login shell has been disabled."
40
41 # Lock the user account
42 sudo pwpolicy -u "$user" -setpolicy "isDisabled=1"
43 echo "User $user has been locked."
44
45 # Uncomment to delete the user instead of just disabling
46 # sudo sysadminctl -deleteUser "$user" -secure
47 # echo "User $user has been deleted."
48 else
49 echo "User $user does not exist or is system-protected."
50 fi
51done
52
macOS - Delete/Disable Users
This Bash script processes a comma-separated list of usernames provided through Level's script variables to perform account disablement on macOS systems. For each username in the list, it performs comprehensive account verification and disablement, including removing Secure Tokens if present, revoking admin privileges, disabling the login shell, and locking the account (with commented code available for full account deletion). The script provides detailed status messages throughout the process, indicating the success of each operation or reporting if the specified user doesn't exist.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.