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

Ljd3636
New Contributor

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

0 REPLIES 0