Subscribe

Get the Network Administrators tool pack

Subscribe to our 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

Recent Posts

Search

Archives

Delete temp files from all user profiles

Post image for Delete temp files from all user profiles

by Steve Wiseman on December 20, 2013 · 19 comments

in Scripts


.

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”

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:

User Profile Delete Temp

You might want to schedule this one as a task that runs on a Sunday night.

You can get a copy of it here:

delete_profile_temp.dat

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

Related Articles:

{ 19 comments… read them below or add one }

1 Hot Dog December 20, 2013 at 11:44 am

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.

2 Steve Wiseman December 20, 2013 at 11:45 am

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

3 Marcus December 20, 2013 at 11:46 am

Hi Steve,

What if those temp files are in use?

4 Steve Wiseman December 20, 2013 at 11:47 am

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.

5 Stu Wilcox December 20, 2013 at 1:52 pm

Doesn’t TFC do this? TFC is available at

http://www.bleepingcomputer.com/download/tfc/

6 Steve Wiseman December 20, 2013 at 2:37 pm

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.

7 Martin December 20, 2013 at 3:45 pm

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.

8 Martin December 20, 2013 at 3:47 pm

Suggestion: How about adding C:\Temp (if it exists) and C:\Windows\Temp.

9 Steve Wiseman December 20, 2013 at 3:53 pm

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

10 Mogens Levin December 21, 2013 at 5:33 am

Sorry, but was unable to download the vb script (delete_profile_temp.dat). I recieved a full admin tool package !!

11 Steve Wiseman December 21, 2013 at 2:42 pm

Hi Mogens,

Send me an email at support@intelliadmin.com and I will give you the direct link.

Thanks

12 Johnny December 23, 2013 at 1:15 pm

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.

13 Steve Wiseman December 23, 2013 at 7:50 pm

Thanks Johnny,

I will make sure to take note and include this in the next version of the script

14 tony January 25, 2014 at 6:56 pm

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

15 Steve Wiseman January 29, 2014 at 1:36 pm

Thanks for the tips tony. Will take these ideas for building the next version of the script.

16 William Jenkins June 16, 2015 at 4:32 pm

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

17 emmanuel April 6, 2016 at 9:25 am

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?

18 Steve Wiseman May 10, 2016 at 12:47 am

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

19 Calum May 20, 2016 at 11:43 am

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.

Leave a Comment

Category Links - Windows Forum - Exchange Forum