Skip to main content
Question

Need a script that will find old users and then delete them

  • December 8, 2021
  • 0 replies
  • 4 views

Forum|alt.badge.img+1

I need a script that will find old users and then delete them.

So far I have this script:

#!/bin/bash

# Loop through users with homes in /Users; exclude any accounts you don't want removed (i.e. local admin and current user if policy runs while someone is logged in)

for username in `ls /Users | grep -v localadmin | grep -v Shared`
do
if [[ $username == `ls -l /dev/console | awk '{print $3}'` ]]; then
echo "Skipping user: $username (current user)"
else
echo "Removing user: $username"

# Optional, removes the account
sudo dscl . delete /Users/$username

# Removes the user directory
sudo rm -rf /Users/$username
fi
done