Posted on 07-12-2017 05:43 PM
I need to delete a local account on a group of Macs. What's the best way to accomplish this? Is there a script I can setup in a policy?
Thanks in advance.
Posted on 07-12-2017 07:39 PM
not sure where I heard it but this line below will remove an account and all other references to it cleanly:
sysadminctl -deleteUser **USERNAME**
you will of course need to put this into a policy etc to run as root
Posted on 07-12-2017 09:02 PM
Posted on 07-14-2017 06:49 AM
Thank you both for the quick response. I was able to follow @jason.bracy suggestion.
Posted on 07-01-2020 09:22 AM
This works for a single known user. Is there a way to delete all my students local accounts/home folders, keeping my Admin in tact and without a full wipe of the machine?
Thanks!
Posted on 07-02-2020 05:39 AM
You can use a for loop.
First up find the list of users
Accounts=$(ls -l /Users | /usr/bin/awk '{print $9}' | /usr/bin/grep -viE '(shared|admin|administrator)')
This will put the account names in the list, and ignore the ones in the end section. Note that the administrator is not really required as the admin will find it.
If you put this into terminal, you will see the results...
ls -l /Users | /usr/bin/awk '{print $9}' | /usr/bin/grep -viE '(shared|admin|administrator)'
Adjust the names in the brackets as you require just put | between each one as a separator.
Then simply use this as the array in a for loop
if [ -z "$Accounts" ]
then
# Null result
echo "Null result for More accounts"
else
# Something there, cycle through them and clean
echo "Found More Accounts, Removing them"
# search through the local accounts
for eachaccount in $Accounts
do
# Delete files here using $eachaccount
# Remove the Homefolder
rm -Rf /Users/“$eachaccount”
# Remove the account
sysadminctl -deleteUser “$eachaccount”
done
fi
Please test and test again before deploying this. It is not a production script from my collection. I have simply put it together for this example.
I do however use a similar, albeit more complicated version, here.
The ls -l command will list all files and folders in the /Users folder, hence shared being in my list. You may also find it pulls in things like .DS_Store .localized and you have to add them in too.