Skip to main content

Command to list all user accounts as well as their last logged in information? [Resolved]

I am using this line of script to return back the list of my user accounts on a Mac via Terminal:

dscl . list /Users | grep -v '_'

While it returns the list of all user accounts, I also need for it to list their last logged in information as well.

I have attempted to research what other commands I could pipe in as well, but am unsuccessful.

How to return a list of user accounts on Mac computer as well as the user account's last logged in information next to it?


Question Credit: Lasagna Cat
Question Reference
Asked June 10, 2019
Tags: , users
Posted Under: Unix Linux
10 views
1 Answers

You could loop over the user accounts and invoke the last command.

Here's a little script:

#!/bin/bash

while read user; do
    if ! last -5 "$user" | grep "^$user"; then
        printf "$user\tno session found\n"
    fi
done < <(dscl . list /Users | grep -v '^_')

You could also feed multiple user names to last like last user1 user2, but you wouldn't get any output unless session data exists for the user.

In the script above option -5 is used to limit the output to the 5 (last recent) entries and grep to make sure each line starts with the user name you're looking for. If no line is found, then no session found is printed for the user.

Make sure you have admin rights when you run the script, otherwise the output of last will be empty.


credit: Freddy
Answered June 10, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA