# Script to check if a user exists on macOS devices

This document provides a guide on checking if a user exists on macOS devices using a script.

Effective user account management is essential for maintaining access control and security. Organizations must ensure that only authorized users have access to their macOS devices. Using a script, IT admins can efficiently check for the presence of a specific user. This verification can be performed remotely with Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action.

Scripting language – Bash

File extension – .sh

 Disclaimer: 
The sample script provided below are adapted from third-party open-source sites.

 

 
 Note: 
The script is supported only on macOS versions 10.14 and above.

 

Check if a user exists
----------------------

Script to check if a user exists on the macOS devices

\#!/bin/bash if id -u "User" >/dev/null 2>&1; then echo "Yes, the user exists." else echo "No, the user does not exist." fi 

   1

2

3

4

5

6

  \#!/bin/bash 

if id -u "User" >/dev/null 2>&1; then

 echo "Yes, the user exists."

else

 echo "No, the user does not exist."

fi 

   

 

  

Replace *‘User’* in the script with the specific username to check. The script will then verify whether the specified user exists on the device.

Here is an example of the script

\#!/bin/bash if id -u "Anthony" >/dev/null 2>&1; then echo "Yes, the user exists." else echo "No, the user does not exist." fi

   1

2

3

4

5

6

  \#!/bin/bash

if id -u "Anthony" >/dev/null 2>&1; then

echo "Yes, the user exists."

else

echo "No, the user does not exist."

fi

   

 

  

**id -u “User” >/dev/null 2>&1; then:**  The **‘id’** command with the **‘-u’** option returns the user ID of the specified username. In the script, **‘>/dev/null 2>&1’** is used to hide both the regular output and error messages generated by the **id** command. This ensures that the output only displays whether the user exists or not, without showing any additional information. The **‘if’** statement verifies the exit status of the ‘id’ command. If the specified user exists, the exit status would be 0, and the script proceeds to the **‘then’** block.

**echo “Yes, the user exists.”** prints a message if the user exists.

[![Execute the script to check user exists on macOS device](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2025/01/Script-to-check-whether-the-user-exists-in-macOS-device.png "Script to check whether the user exists in macOS device")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2025/01/Script-to-check-whether-the-user-exists-in-macOS-device.png)

**echo “No, the user does not exist.”** prints the message if the user does not exist on the macOS device.

[![Output showing the user does not exist after executing the script](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2025/01/Script-to-check-the-user-exists-in-macOS-device.png "Script to check the user exists in macOS device")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2025/01/Script-to-check-the-user-exists-in-macOS-device.png)

 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.