Problem overview
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.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Define the users to remove
USERS_TO_REMOVE="{{UsersToDelete}}"
# Convert the comma-separated list into an array
IFS=',' read -r -a userArray <<< "$(echo "$USERS_TO_REMOVE" | sed 's/, */,/g')"
for user in "${userArray[@]}"; do
# Trim spaces from the username
user=$(echo "$user" | xargs)
# Check if user exists
if id "$user" &>/dev/null; then
echo "Processing user: $user"
# Check if the user is in the admin group and remove from it
if dscl . -read /Groups/admin GroupMembership 2>/dev/null | grep -q "\b$user\b"; then
echo "Removing $user from the admin group..."
sudo dseditgroup -o edit -d "$user" -t user admin && echo "$user removed from the admin group."
fi
# Get user's UUID
UUID=$(dscl . -read "/Users/$user" GeneratedUID 2>/dev/null | awk '{print $2}')
# Remove UUID from admin group if it exists
if [[ -n "$UUID" ]]; then
echo "Removing UUID $UUID from admin group..."
sudo dscl . -delete /Groups/admin GroupMembers "$UUID" && echo "Removed UUID: $UUID from admin group."
fi
# Delete the user account
echo "Deleting user $user..."
sudo dscl . -delete "/Users/$user" && echo "Deleted user $user."
else
echo "User $user does not exist. Skipping..."
fi
done 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.
Use cases
- Disabling unauthorized or orphaned macOS user profiles
- Quickly locking out dormant accounts that pose security risks
- Streamlining compliance for macOS environments by removing inactive or admin-privileged users
- Combining with admin compliance workflows for a unified security strategy
Recommendations
- Configure a script-based monitor in Level to automatically disable or remove users detected as unauthorized
- If ongoing checks are needed, create a Level automation with a schedule trigger to run regular cleanups
- Test the script in a non-production environment first to validate the behavior and confirm that password parameters (if needed) are set correctly
- Uncomment the relevant line to permanently delete users if that aligns with your organization’s security policy
- Check out the Admin Compliance & Remediation Automation