I got a great question from Brian this week:
“Is there a quick way to delete the temp files for all user profiles?
We have some PC’s that many users logon to and over a period of time there can be many thousands of temp files in each users profile.”
A simple way to do this is to write a script that will go through all the profiles.
Where do we start?
Well, first we want to get a list of all folders under “C:\Documents and Settings”
This will give us a list of the root user profile folders.
From there, we need to enumerate all the files and folders under this path of each profile:
"C:\Documents and Settings\[username]\AppData\Local\Temp"
Now that we have that list of files, we can simply delete them to clear out all the temp files.
Sounds easy, but it does require a little bit of work in VB Script. The script I built follows what I said, but it also automatically detects “C:\Users” used under Windows 8, and 2012.
To delete temp files in all profiles you need to run it as an administrator, like this from the command line:
cscript.exe delete_profile_temp.vbs
Careful, since once you start the script – it will quickly delete those temp files:
You might want to schedule this one as a task that runs on a Sunday night.
You can get a copy of it here:
Remember to rename it to .vbs 🙂
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools
{ 19 comments… read them below or add one }
Holy Cow! I took a look at that script, and it is much more complicated than I thought. Thanks for writing this one up steve.
Hi Hot Dog,
Mostly this is because VBScript is lacking in some areas…specifically dynamic arrays. It requires some work to get dynamic arrays functioning so the list of folders and files can be built up.
Steve
Hi Steve,
What if those temp files are in use?
Hi Marcus,
It is a good idea to not run it while you have a bunch of programs up and running. The best time would be late at night when nobody is using the computer. Mainly because…deleting temp files of applications while they are running will cause undefined behavior. You could lose stuff, or the apps could crash.
Doesn’t TFC do this? TFC is available at
http://www.bleepingcomputer.com/download/tfc/
Hi Stu,
Looks like a cool app. In this article I wanted to do it in VBScript, so others could see the code and morph or changes it to fit their own needs.
Great idea but after clicking through 50 or so prompts telling me what had been deleted, I had to End Task the script. Can’t really automate it to run late at night with all those prompts. I disabled them easy enough but now would like a confirmation when it’s finished. Not sure where to insert the code to do this.
Suggestion: How about adding C:\Temp (if it exists) and C:\Windows\Temp.
Hello Martin,
You want to call it with cscript.exe…so it prints to the screen instead of showing popup boxes.
It would look like this on the command line:
c:>cscript.exe delete_profile_temp.vbs
Sorry, but was unable to download the vb script (delete_profile_temp.dat). I recieved a full admin tool package !!
Hi Mogens,
Send me an email at support@intelliadmin.com and I will give you the direct link.
Thanks
Hi Steve,
There is another temp file location in the profile:
C:\Documents and settings\User name\Local Settings\Temp
You might want to add that.
Thanks Johnny,
I will make sure to take note and include this in the next version of the script
Corrected
Steve
Nice script, just a couple notes.
1. C:\temp is used by many (instead of C:\Windows\Temp).
2. You are not deleting temporary Internet Files.
XP: ‘profilename’\Local Settings\Temporary Internet Files
7: ‘profilename’\appdata\Local\Microsoft\Windows\Temporary Internet Files
3. Although I have not tried this on windows XP yet. I cannot see it working. You set the user’s temp folder as ‘profilename’\appdata\local\temp. This is a Windows Vista/7/8 folder. in XP the folder would be ‘profilename’\Local Settings\temp
Thanks for the tips tony. Will take these ideas for building the next version of the script.
While I do like this script, there is a really nice freeware tool called ICSweep (was written to help Terminal Server/Citrix admins) that does this very quickly and cleanly that I’d recommend looking at. http://www.ctrl-alt-del.com.au/files/ICSweep.txt
Hi
on Windows 7 Pro 64 bits, I get this:
C:\>cscript.exe c:\delete_profile_temp.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation 1996-2001. Tous droits réservés.
c:\delete_profile_temp.vbs(36, 3) (null): the file name can’t be resolved by the system
This is a french system so profiles name may contain space, accent, …
How to fix it?
I am not sure on that…but it might not be the issue. Check the name of the path in the script and make sure it matches your French version
Hello, I’ve been using the following syntax:-
@echo off
if %os%==Windows_NT goto WINNT
goto NOCON
:WINNT
echo .Using a Windows NT based system
echo ..%computername%
echo Deleting Remote Desktop Cache
del /q /f /s “%username%\AppData\Local\Microsoft\Terminal Server Client\Cache\*.*”
pause
echo Deleting Profile Temp Files
del /q /f /s “%USERPROFILE%\AppData\Local\Temp\*.*”
pause
goto END
:NOCON
echo Error…Invalid Operating System…
echo Error…No actions were made…
goto END
:END
I’m trying to run this as a .bat file on a Windows 2012 R2 server, but it fails as not all the of user profiles have the path to the Terminal Server cache folder. Anyone any idea’s how I can over come this as it’s just really TS cache folder that I need to clean out on the user profiles.