Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Disk Cleanup and SMS

Posted on 2009-02-09
Medium Priority
Last Modified: 2012-06-21
I have an issue with the disk cleanup utility. I have created an SMS package to deploy the package and run the Disk cleanup. The problem I did not think of at first when I was creating the package was how it would cleanup the principle user's profile's temp directory since it is running as administrator. How would I get it to clean up the temp directories in user profiles other than admin, or is it possible? If not, is there a utility available that can do this? Thanks.
Question by:Mickmeister
  • 3

Accepted Solution

ACollyer earned 500 total points
ID: 23596556
There are some things you will need to clean up as Administrator or System (Downloaded Program files, Setup Log Files, Compress Old Files etc), so you will need to keep your deployed package in place if you want to clean up these. You are correct in saying that for files like Temporary Internet Files, you will need to run Disk Cleanup under the context of the user whose profile you wish to clean.

I would recommend you do this in a logoff script so it runs at the end of the user's session, just before shutdown/logoff.

The Disk Cleanup util, cleanmgr.exe, has command line switches you can make use of here. Before you script cleanmgr.exe to run, you first need to configure a Disk Cleanup profile specify which files you want to clean up. This is a per-workstation setting, but conveniently it's stored in the registry so isn't much of a problem.

Disk Cleanup profiles are numbered (integer between 0 and 65535) so basically what you need to do is set those registry keys before running the util itself. This can be done with a Startup script. Startup scripts are run as System and therefore can write to the HKEY_LOCAL_MACHINE registry hive where the Disk Cleanup profiles live (regular users cannot.) All that's required is a simple batch file containing the line:

regedit /s \\myserver\myshare\myregistrysettings.reg

Save the batch file somewhere convenient like the NETLOGON share on your domain controller, and configure it in a group policy applied to the OU containing your workstations (Computer Configuration --> Windows Settings --> Scripts --> Startup.) This will configure the Disk Cleanup profile before the user logs on.

I have attached a zip file containing a suitable Disk Cleanup profile for you (you will need to change the file extension to .reg.) Please review the file before running it! It's fine, but you should always know what you're running on your system.

The following user-specific files are enabled for cleanup: Temporary Internet Files, Recycle Bin, Remote Desktop Cache Files, Temporary Files, WebClient and WebPublisher Cache.
If you want to disable any of these, you can change the "StateFlags0001" value in the relevant regkey to dword:00000000 instead of dword:00000002.

Needless to say, you should manually apply this to a single workstation and test thoroughly before mass deployment, and of course always backup the registry before making changes (although I have tested this file on WinXP and it's harmless.)

Ok, now you have your workstations configured with a Disk Cleanup policy, you can now configure the logoff script for your users. You might have a logoff script in place already, in which case you can just add this line to the batch file:

cleanmgr.exe /sagerun:1

The "sagerun" switch selects which policy number to use. In this case, policy 1 is used as this is what I have configured in the attached reg file. If you don't have a logoff script configured, you should create a group policy on the OU containing your users and follow the same steps as for the startup script, except you will want to configure User Configuration --> Windows Settings --> Scripts --> Logoff.

Ok, there are quite a few instructions there, but hopefully it all makes sense. If you need clarification on anything, please please ask before trying it out.
Good luck :)

Expert Comment

ID: 23596569
Here's a bit more info on cleanmgr.exe: http://support.microsoft.com/kb/315246

Expert Comment

ID: 23653349
How are you getting on with this, Mickmeister? Please feel free to share any problems you come across.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back when OPSMGR 2012 was released we were very excited about getting it into our environment and upgrading our 2007 implementation,  we started our planning and we then proceeded with our implementation. All went as planned & our system …
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question