# Script to view Login History in macOS devices

When multiple users work on the same macOS device, it is advisable to record the history of all the users logged in to the device. This could help you in identifying the user who was using the device during a specified time. You can seamlessly fetch the login history of your macOS devices using Hexnode’s [Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) feature.

 Disclaimer:The Sample Scripts provided below are adapted from third-party Open-Source sites.

 

### View a brief login history

Script to view a brief login history on a macOS device

\#!/bin/bash Who 

   1

2

  \#!/bin/bash 

Who 

   

 

  

You will get a brief history of all the users that have logged in to your macOS device in the past few days. It returns the login time and name of the user who has logged into the device.

### View a detailed login history

Script to view a detailed login history on a macOS device

\#!/bin/bash last 

   1

2

  \#!/bin/bash 

last 

   

 

  

You will get a detailed list of all the users that have logged in to your macOS device in the past few days. It returns the date, the login and logout time of every session, as well as the name of the user who has logged in to the device during each session.

Obtain the last logged in time of a user 
-----------------------------------------

If you want to check the last logged-in time of a particular user, you can use the following script.

Sample script to retrieve the last login time of a given user

\#!/bin/sh #!/usr/bin/env bash last -1 username 

   1

2

3

  \#!/bin/sh 

\#!/usr/bin/env bash 

last -1 username 

   

 

  

Ensure you specify the user’s name is intact. Obtain the actual username of a user using the following script:

Sample script to list all the users on a given Mac

\#!/bin/sh #!/usr/bin/env bash dscl . list /Users 

   1

2

3

  \#!/bin/sh 

\#!/usr/bin/env bash 

dscl . list /Users 

   

 

  

It list all the users on a given machine.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.