Skip to content

Instantly share code, notes, and snippets.

@talkingmoose
Last active April 3, 2024 10:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save talkingmoose/4be6ae23c687469098c43fb6f9c06eab to your computer and use it in GitHub Desktop.
Save talkingmoose/4be6ae23c687469098c43fb6f9c06eab to your computer and use it in GitHub Desktop.
Simple script to create a new macOS user account. Will not provide a SecureToken.
#!/bin/zsh
# new user account details
username="lapsadmin"
displayName="LAPS Admin"
password="P@55w0rd"
admin="yes"
hidden="yes"
# determine next available UID
highestUID=$( dscl . -list /Users UniqueID | /usr/bin/awk '$2>m {m=$2} END { print m }' )
nextUID=$(( highestUID+1 ))
# create the account
/usr/bin/dscl . create "/Users/$username"
/usr/bin/dscl . create "/Users/$username" UserShell /bin/zsh
/usr/bin/dscl . create "/Users/$username" RealName "$displayName"
/usr/bin/dscl . create "/Users/$username" UniqueID "$nextUID"
/usr/bin/dscl . create "/Users/$username" PrimaryGroupID 20
/usr/bin/dscl . passwd "/Users/$username" "$password"
# make the account admin, if specified
if [[ "$admin" = "yes" ]]; then
/usr/bin/dscl . append /Groups/admin GroupMembership "$username"
fi
# hide the account, if specified
if [[ "$hidden" = "yes" ]]; then
/usr/bin/dscl . create "/Users/$username" IsHidden 1
/usr/bin/dscl . create "/Users/$username" NFSHomeDirectory "/private/var/$username"
else
/usr/bin/dscl . create "/Users/$username" NFSHomeDirectory "/Users/$username"
fi
@abeazam
Copy link

abeazam commented Mar 3, 2023

This worked a treat but as soon as I reboot the mac the option for other seems to disappear as if it’s only in ram

@talkingmoose
Copy link
Author

@abeazam I'm surprised this still works, but I just tested and pleased to see it does. Since the button to select "Other" is missing from your login window, I suspect your Mac is encrypted using FileVault and that you're seeing the FileVault screen not the macOS login window. This script doesn't enable the new user account to unlock FileVault.

@abeazam
Copy link

abeazam commented Mar 4, 2023

@talkingmoose thanks for the info

@kumarjonwal91
Copy link

while running this script, I am facing an issue where it doesn't create Home directory for the new user. It creates the new user with Admin privileges but no directory for that user.
FYI, I have FileVault enabled and I am deploying it via JAMF.

@talkingmoose
Copy link
Author

@kumarjonwal91 I believe logging in to the account from the login window used to create the home directory folder structure, which may be why the script doesn’t create the home directory. Not sure if this behavior has changed with newer versions of macOS.

You can add these lines toward the end of the script to manually create it from the User Template.

/usr/bin/ditto "/Library/User Template/Non_localized" "/private/var/$username"
/usr/bin/chown -R “$username" "/private/var/$username"

@BDat80
Copy link

BDat80 commented Mar 27, 2024

Hey, I'm very knew to the world of Bash, and when I try to launch your code I have an error this kind of error:

passwd: Invalid Path

attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError) attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError) attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError) attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError) attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError) DS Error: -14009 (eDSUnknownNodeName) attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError) attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError) attribute status: eDSPermissionError DS Error: -14120 (eDSPermissionError)

Could you help me to understand?
Thanks.

@talkingmoose
Copy link
Author

@BDat80, I believe you're not running the script with administrator privileges based on the feedback you're receiving.

Try this:

  1. Make sure you're logged in to your computer as an administrator.
  2. Save the script to a plain text file on your Desktop and name it something like "CreateUser".
  3. Open Terminal and enter "sudo" (superuser do) and drag the "CreateUser file into Terminal to fill out the full path for you automatically. It'll look something like sudo /Users/username/Desktop/CreateUser.
  4. Press return and enter your administrator password when prompted.

@BDat80
Copy link

BDat80 commented Apr 3, 2024

Thanks a lot @talkingmoose, this is working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment