So here is the scoop. I want to make a script to delete the temp folder in each users profile C:\Users\Default\AppData\Local\Temp). We have about 10 user profiles on each pc so manually doing this just isn’t in my wish list.

What would be the best way to capture each user profile name and insert it into a delete command? My thought was to somehow output a “dir” command to a text file and suck it into a looping script to del C:\Users%username%\AppData\Local\Temp\ but I dont have any idea of how to do that.

Thanks in advance.

6 Spice ups

http://gallery.technet.microsoft.com/scriptcenter/Delete-Temp-files-2f2438f1

Set fso = CreateObject("Scripting.FileSystemObject")
Set base_folder = fso.GetFolder("C:\Users\")
Set folders = base_folder.SubFolders
 
For Each folder In folders
 
    If fso.FolderExists(folder & "\AppData\Local\Temp") Then

        fso.DeleteFolder folder & "\AppData\Local\Temp\*"
        fso.DeleteFile folder & "\AppData\Local\Temp\*"

    End If

Next

Heres a start.

3 Spice ups

That link will help get you started with a vbscript – you might need to add a bit to get the Appdata\local\temp folder deleted as well, but this gives you the necessary syntax, etc.

Set it up as a script that runs at logoff via GPO.

del %userprofile%\Appdata\local\temp . /y

http://community.spiceworks.com/how_to/show/2049-delete-temp-files-with-single-command

You can use GPP.

http://community.spiceworks.com/topic/117488-delete-temp-folde

1 Spice up

Add to logon or logoff script:

rd /q /s "%temp%" & mkdir "%temp%"

Just use a loop to go through all the folders in the C:\users directory and delete anything in temp. No need to figure out which profiles exist on a the computer beforehand.

You got it! Super easy to modify and the beginning is exactly what I was looking for to hit all user profiles at one time rather than having it run only for the current user. Thanks for the help.

first off as long as the profile is in use deleting parts of it will not work properly, files remain in use etc.

So best is to do this when the user logs off.

When you’re using roaming profiles you can use a gpo to do this, just goole “delete cached copies of roaming profiles”

You can also do this with a custom script, personally I advocate keeping this within the standard so if something happens to you and they need to replace you the next guy can continue where you left off. All those creative self made workarounds become legacy problems after a while.

A neat trick is to use uphclean (it’s free and downloadable from technet) it basically forces the unmounting of the ntuser.dat, if you don’t you’ll start noticing empty profile folders with numberings at the end, Wich is windows trying to delete a cached profile while winlogon is still busy logging the user off. In short it saves you from having to boot the system before you can delete a profile (or part of it).

Another idea is to use a tool like ccleaner and shedule it.