Category filter

Script to create new users on Mac

If a user on Mac has the admin role, they can create a new user easily from System Preferences > Users & Groups. But, when a device admin managing many macOS endpoints desires to create a new account this way, it becomes a tedious process to do it manually on each endpoint. For such scenarios, you can use the script below to create new users in batch on Mac.

Device admins can remotely run scripts on Macs managed with Hexnode using the Execute Custom Script action.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Create a new user using the dscl command

The dscl command is a command line utility that helps create/modify user accounts.
When you add the Unique ID and Primary Group ID, note the following points –

  • The UniqueID for a user must be unique to the user.
  • You can set the PrimaryGroupID as ‘80’ to add the user to the Admin user group directly. Or, set the PrimaryGroupID as ‘20’ to add the user to the Standard user group.
Note:


The dscl commands might not run as expected on some macOS versions.

Create a new user using the sysadminctl command

The above script uses the sysadminctl command to create a new user on Mac.

Note:

In Bash, before inserting space while defining file or folder names, we use a backslash \ to separate the characters. This will prevent the shell interpreter from interpreting the space as a separator and assuming they were two different arguments. Hence, we write New user as New\ user in the above code.


If your system is FileVault encrypted, only FileVault enabled users will show up on the initial login screen after reboot. To add a user to the login screen, the user will have to be manually enabled by the device administrator to unlock the disk from System Preferences > Security & Privacy > FileVault > Enable Users.

You can also run the below script to do the same –

sudo fdesetup add -usertoadd New\ user

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository