Removing Local User Profiles

SMC
New Contributor

Hi all,

Is there a way within Casper to remove all local user profiles except for the local admin account?

Was wanting to do this using a Policy or Configuration Profile

Much appreciated

4 REPLIES 4

davidacland
Honored Contributor II

Do you mean the user home folders in /Users? If so you can use something like this:

counter=`ls /Users | grep -v -E ‘grep -v ‘Shared|Guest|.localized|.DS_Store|localadmin’ | grep “[A-z 0-9]” | grep -c "[A-z 0-9]"`
     # Outputs the number of folders in the /Users directory, excluding the Shared, Guest and localadmin directories and the .ds_store file.

# Loop start

     while [ $counter -ne 0 ]
          do
               targetFolder=`ls /Users | grep -v -E ‘grep -v ‘Shared|Guest|.localized|.DS_Store|localadmin’ | grep “[A-z 0-9]”  | head -$counter | tail -1`
                    # Gets the target folder name

               # Action to take on target folder $targetFolder
                    # Deletes the target folder

               counter=$(( $counter - 1 ))
                    # Reduces the counter by 1
     done

exit 0

On the line "# Action to take on target folder $targetFolder", you would add in something like "rm -rf $targetFolder".

I've intentionally left that bit out, incase anyone grabs this script in the future without reading it ;).

DaneEckert
New Contributor

We use the the script referenced here. https://jamfnation.jamfsoftware.com/discussion.html?id=4502

May need to modify it based on what UID your local admin account has.

SMC
New Contributor

@davidacland Exactly what I meant however I'm getting this message where I run it.

I've slightly modified the code to exclude only the 'Shared' folder.

counter=ls /Users | grep -v -E ‘grep -v ‘Shared’ | grep “[A-z 0-9]” | grep -c "[A-z 0-9]"

while [ $counter -ne 0 ] do targetFolder=ls /Users | grep -v -E ‘grep -v ‘Shared’ | grep “[A-z 0-9]” | head -$counter | tail -1

rm -rf $targetFolder

counter=$(( $counter - 1 )) done

exit 0

Running script Delete Local User Profiles... Script exit code: 0 Script result: grep: ‘Shared’: No such file or directory grep: brackets ([ ]) not balanced Submitting log to https://server.local:8443/ Finished.

davidacland
Honored Contributor II

Here's what you need:

counter=`ls /Users | grep -v Shared | grep "[A-z 0-9]" | grep -c "[A-z 0-9]"`

while [ $counter -ne 0 ] 
    do 
        targetFolder=`ls /Users | grep -v Shared | grep "[A-z 0-9]" | head -$counter | tail -1`
        rm -rf /Users/$targetFolder
        counter=$(( $counter - 1 )) 
    done

exit 0