Disk Cleanup and SMS

Posted on 2009-02-09
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
    LVL 6

    Accepted Solution

    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 :)
    LVL 6

    Expert Comment

    Here's a bit more info on cleanmgr.exe:
    LVL 6

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
    I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now