Back to Resources
Level
Script
Security
Devices locked during emergencies or security incidents need a swift and reliable way to restore access once the threat is addressed. This script provides a straightforward solution by unlocking all local user accounts on a macOS endpoint, saving valuable time and effort.
This script scans through all valid local user accounts on a macOS device and systematically enables each one. It works in tandem with our macOS Lock Device Script, allowing you to confidently lock down a device when necessary and then revert it to normal operation with a single command. Throughout the process, the device remains connected to Level, enabling full remote control and management.
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-unlock-device
10
11# Initialize script-scoped error flag
12script_errors=false
13
14# Function to enable all local user accounts (INCLUDING ROOT)
15enable_local_accounts() {
16 echo "Enabling all local user accounts (including root)..."
17 local_users=$(dscl . list /Users | grep -vE '^(Guest|nobody|_.*|daemon)$')
18
19 for user in $local_users; do
20 sudo pwpolicy -u "$user" enableuser &> /dev/null
21 if [[ $? -eq 0 ]]; then
22 echo "Local account $user has been unlocked."
23 else
24 echo "ALERT: Failed to unlock local account $user."
25 script_errors=true
26 fi
27 done
28}
29
30# Execute actions
31enable_local_accounts
32
33if [[ "$script_errors" == true ]]; then
34 echo "ALERT: Errors occurred during execution. Exiting with code 1."
35 exit 1
36else
37 echo "All users have been unlocked successfully."
38 exit 0
39fi
macOS - Unlock Device
This script enables all local user accounts, including root, on a macOS system by modifying user policies. It is useful for restoring access after a previous lockdown but should be used with caution to avoid enabling unwanted accounts.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.