NOTE: Due to macOS Monterey's prompts when Python 2 in invoked, please review the blog post here to get the logged in user instead.

When running scripts via Jamf Pro, Munki or a launch daemon, you sometimes have a challenge to get the username of the currently logged in user.
Below has a method to achieve this, with a link to an Apple approved method.
The challenge
If you’re running a script via Jamf Pro at login or via Self Service, then $3 can be used to grab the username.
However, what if you need to check at another time or you don’t have Self Service?
A common method used is below & you’ll find it in a number of my scripts.
loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
The recommendation
On seeing an example of the above, Frogor over at the ##OSX-Server IRC mentioned that this method was both a little “odd” & not Apple’s recommended way. Then, being the guy he is.. He not only pointed us in IRC to a link about Apple’s recommended method, but also gave an example of how to call it within bash. That example is below:
loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
You’ll start to see the above soon in my new posts, & if you do use Fast User Switching then this may work better for you where other methods have failed.
That’s nice and simple. Holy hell.
Awesome! Thanks for sharing!
Thanks for that! It seems to run a bit slow for me compared to some of the old ways of getting the name. Was that the same for you?
Ish.. but it works, so happy to wait!
is not easier to use Last User from build in Extension Attributes in JSS?
Whilst that works, it’s only as good as your last recon & depends on when that is.
The above can be run as part of a script whenever.
How about doing this?
who | grep “console” | awk ‘{print $1}’
Can give false results if you have more than one user logged in.
Dude! Thank you for sharing this!
Best Practice is also to use $(do stuff here) instead of `do stuff here`. If that got reformatted, replace the back-ticks surrounding the python code with a dollar+open parenthesis and end the block with a close parenthesis.
Currently I cannot see why not: loggedInUser=$(id -un)
Doesn’t work when run as root
Make sure specify /usr/bin/python:
`/usr/bin/python -c ‘from SystemConfiguration…
Specify the full path to python or it might use a /usr/local/bin/python and not load the modules that Apple provides with their customized python.
What’s odd is the Technical Q&A link states:
– It has no way of indicating that multiple users are logged in.
– It has no way of indicating that a user is logged in but has switched to the login window.
Perhaps I’m misunderstanding?
Forgot to add the line from the Q&A: “Because of these issues, routines like SCDynamicStoreCopyConsoleUser exist only for compatibility purposes. It’s likely that they will be formally deprecated in a future version of Mac OS X.”
This worked excellent!
Wondering if there is a method to AWK out the first name instead of the entire name.
So it would say “Welcome John”. Instead of “Welcome John Smith” or “Welcome Johns MacBook Air”
I am no scripter by far, so I relay on the community to fill in what I do not know.
Thanks People.
This may be deprecated as Catalina 10.15+ approaches, and Python may be no longer be included in macOS.
Let’s all put a nail in the coffin of the Python method and agree to use this elegant stat based method:
consoleUser=$(stat -f %Su /dev/console)
It’s so nice and compact.
It works with fast user switching.
You can even commit it to memory!
At the login screen it will return “root”, the Python method will not return anything.
It’s literally ~130x faster since it doesn’t have the overhead: Python=.528s, stat=.004s
…and it almost never fails. Almost.
This is the method I use as well. I hate the idea of calling python, but definitely wanted someting more reliable than ls.