Tuesday, August 5, 2008

Creating a new user remotely via command line

I had trouble getting the home directory to be created properly in some cases. Please test on
one machine before you blast through to your lab. This creates a new user called 'New User'
with a shortname of 'newuser'.

dscl / -create /Users/newuser
dscl / -create /Users/newuser UserShell /bin/bash
dscl / -create /Users/newuser RealName "New User"
dscl / -create /Users/newuser UniqueID 510
dscl / -create /Users/newuser PrimaryGroupID 1000
dscl / -create /Users/newuser NFSHomeDirectory /Users/newuser

This next line creates the password exactly as you type it. It is case sensitive. The sample creates the password 'PASSWORD' in uppercase.

dscl / -passwd /Users/newuser PASSWORD

If you want to add admin privileges to the user:

dscl / -append /Groups/admin GroupMembership newuser

Logging in as a user via commandline to the loginwindow

This is from macoshints.com.  I could not get it to work past bringing
up the users login name and presenting the password field, but it may
provide some bread crumbs for the adventurous.

osascript -e 'tell application "System Events" to keystroke "LOGIN_NAME"'; \
osascript -e 'tell application "System Events" to keystroke tab'; \
osascript -e 'tell application "System Events" to delay 0.5'; \
osascript -e 'tell application "System Events" to keystroke "PASSWORDHERE"'; \
osascript -e 'tell application "System Events" to delay 0.5'; \
osascript -e 'tell application "System Events" to keystroke return'