(macOS) Create a new user worklet

I'm exploring a custom worklet to create a new user.

So far this has been successful creating the user.
However when trying to login, the password does not work.

Please help if you think of a way around this.

#!/bin/bash
sudo ddcl . -create /Users/automox
sudo dscl . -create /Users/automox UserShell /bin/bash
sudo dscl . -create /Users/automox RealName Automox
sudo dscl . -create /Users/automox UniqueID 9999
sudo dscl . -create /Users/automox NFSHomeDirectory /Local/Users/automox
sudo dscl . -passwd /Users/automox password123
sudo dscl . -append /Groups/admin GroupMembership automox

ps - Don’t use password123… I’m sure that goes without saying :slight_smile: Also there’s better ways to make users, but we’re trying to conjure this up for a specific use case.

3 Likes

great work here cfrieberg, dscl is indeed the preferred method for creating user accounts on macOS. One limitation of that tools is that it does not create a user’s home folder. Based on the last line it seems like you want this account to have admin rights, so I fixed that below as well.

#!/bin/bash

user_short_name="automox"
user_long_name="Automox"
user_password="Password123"
user_unique_id="9999"

dscl . -create /Users/"$user_short_name"
dscl . -create /Users/"$user_short_name" UserShell /bin/bash
dscl . -create /Users/"$user_short_name" RealName "$user_long_name"
dscl . -create /Users/"$user_short_name" UniqueID "$user_unique_id"
dscl . -create /Users/"$user_short_name" NFSHomeDirectory /Users/"$user_short_name"
dscl . -passwd /Users/"$user_short_name" "$user_password"
dscl . -create /Users/"$user_short_name" PrimaryGroupID 20 #for an admin user, use 80 for a standard user
cp -R /System/Library/User\ Template/English.lproj /Users/"$user_short_name"
chown -R "$user_short_name":admin /Users/"$user_short_name"
6 Likes

This is great! Thank you Tim.

I tested it out in a custom worklet, it successfully created a user account that I could log into.

I wonder if there’s a way to automate the account setup steps (siri, language, etc that come up after loggin in)

Thanks again!

2 Likes

There are ways to force a password reset on next login, if that seems like a nice feature to add.

1 Like