Software

How do I add Defragment and Disk Cleanup to the right-click menu in Windows Explorer?

In this How do I blog post Jack Wallen shows you how to create right-click menu entries for both Defragmenting and Disk Cleanup.

If you do any consulting or if you are an administrator of enough personal computers, you know that having to go through the process of defragmenting or cleaning up a hard drive is a time-consuming ordeal. Yes, you can schedule these tasks so that they are handled automatically. But what if you need to do them manually? What if you have to do this to a number of machines? If that's the case, we're back to consuming your time. Anything that can make this task easier would be a welcome relief. Fortunately there is a way you can cut down the steps for both of these tasks.

In this How do I article, I will show you how to create right-click menu entries for both Defragmenting and Disk Cleanup that will show up when you right-click a drive in Windows Explorer. With these menu entries, you can avoid having to go through the steps of locating these tools from within the Microsoft Windows Start menu. I will warn you that in order to add these entries you will have to use the registry editor tool, so make sure you make a backup of your registry, just to be safe. As we all know, a corrupted registry can wreak havoc on your Windows machine. So back it all up before you undertake this task. With that said, let's get down to business.

This blog post is also available in PDF format in a free TechRepublic download.

Open up the registry editor

This step should be old hat for most of you. Click Start | Run (Windows XP) or Start and then enter regedit in the run dialog (Vista and 7). This will open the registry editor for you. One of the first things you should do, once this is open, is back up your registry:

  1. Select the top level in the registry (should be labeled Computer).
  2. Click File | Export.
  3. Navigate to where you want to save the backup.
  4. Give the backup a unique name.
  5. Click Save.

That's it.

Add commands

First, let's create the menu entry for the Defragment option. Once you have this done, the Disk Cleanup is just as easy. The first thing you need to do is to navigate (within the registry editor) to HKEY_CLASSES_ROOT\Drive\shell. Create a new key under this section by right-clicking shell and selecting New | Key. When you create the new key, you will want to name the new key runas and the default value Defragment. To change the default value, right-click the runas key and select Modify (Figure A).

Figure A

Create the runas key.
Now select the runas key, right-click it, and create a new key. This key will be named command and will have a default value of defrag %1 -v. You can see how this will look in Figure B.

Figure B

Make sure the keys are children of the correct parent.
Now if you open up Windows Explorer and right-click a drive, you will see Defragment in the menu (Figure C).

Figure C

Add Defragment to the menu.

When you click on the Defragment option, you may (depending on the version of Windows you use) have to give Windows permission to run the task.

Now it's time to do the same thing for Disk Cleanup. You already have a backup of your registry, so you can skip that step. The steps for this are pretty much the same as for Defragment. The first thing to do is navigate back to HKEY_CLASSES_ROOT\Drive\shell.

Create a new key under shell with the name diskcleanup and a default value of Disk Cleanup (Figure D).

Figure D

Add a new key called Disk Cleanup.
Now create a child key of diskcleanup with the name command and a default value of cleanmgr.exe /d %1. Figure E shows what these keys will look like.

Figure E

Add Disk Cleanup to the menu.

Just like you did with the Defragment entry, make sure that the Disk Cleanup entries are in the proper order.

Go back to Windows Explorer (My Computer) and list your drives. Right-click one of those drives, and you will see the Disk Cleanup menu entry right next to the Defragment entry (Figure F).

Figure F

Cleanup and Defrag are now both right at your fingertips!

If you don't want your users knowing these options are available, you can always hide them so that they appear only when you hold the Shift key down and right-click a drive. To do this, you will add an additional key (under either runas or diskcleanup) called Extended with no value.

Final thoughts

You now have quick access to two Windows tools that are necessary and should be quick to open. Most people don't realize that the right-click menus can be edited -- even though it must be done through the registry. Fortunately the process is not terribly painful. And although this process involved editing the registry, you backed up just in case, right?

Stay on top of the latest Microsoft Windows tips and tricks with TechRepublic's Windows Desktop newsletter, delivered every Monday and Thursday. Automatically sign up today!

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

20 comments
shadzovgra
shadzovgra

Damned good idea. Sure you can use the run command prompt to make windows 7 defragment a disk, but the right click option is quicker and as for the let it run after allowing setting a scheduled time, that's a pain in the ass if you're a gamer and you forget exactly what time and day the inbuilt defragmenter is going to run. I like the idea for those who don't use a third party tool to do the job. Personally I use Ashampoo Magical Defrag to do the job. It's a set and forget defragmenter that only runs whenever there is no mouse activity. Then when it's finished your files are protected against further fragmentation. Nice hint though, well done to TR Dojo.

bjr1214
bjr1214

First of all you will need to assign the desired drive letter in the registry. So you would have to create multiple keys to scan multiple drives. for example create a key "Defragment C", create a key "Defragment D" and so on. However if you only want to defragment a single drive, you may find this useful. If you would like to run your own defrag utility rather than the useless (IMO) windows supplied utility, the same can be achieved if the utility has a command line feature. In this example I used "Defraggler" Follow the steps above however rather than entering the "defrag %1 -v" value, point it to the defraggler command line tool. This is found by default in the "C:\Program Files\Defraggler" folder. I used "C:\Program Files\Defraggler\df.exe c: /Large *.* 300" this defrags the C Drive and moves large files 300Mb or over to the end of the drive, therefore speeding up operations. You could also simply use "C:\Program Files\Defraggler\df.exe c:" I hope this helps someone

Who Am I Really
Who Am I Really

adding defrag is OK but I strongly urge people not to use Disk Cleanup with their systems if there are any media files stored on the system. it may very well be fine for a normal workstation but I know for a fact that Disk Cleanup has hosed many a media file by compression Disk Cleanup has a function that is selected by default (or was the last time it opened against my will) "Compress Older Files" it sets a registry item and then proceeds to snoop the drive on a daily basis for files which have an old last access stamp and then compresses the file(s) not good for your .wav .mp3 .wma .wmv .ogg .mov .mp3 etc. files as they end up corrupted in the process I know because it happened to my media workstation about 4 years ago and I haven't ever allowed disk cleanup to run on any system since I lost several GB of files

bkamlin
bkamlin

Go to Run type cmd then type defrag C: /f press enter It works with all versions of windows.

ph81
ph81

It is very important to defrag your PC's at least once a month this speeds up the file system and organizes the files so the OS locates them quicker.

butchy07
butchy07

I have Vista, the Diskcleanup works, the Defragment doesn't. Instead of what's described I get Defragment with a window's security icon and when selected it opens a command prompt window, what'd I do wrong?

mistercrowley
mistercrowley

What about XP users? I have not jumped on the 7 bandwagon yet.

tebbec
tebbec

Can I set it to run Defrag as the administrator?

JudZilla
JudZilla

Hi, I tried the defrag one and it didn't work because the final parameter was incorrect. Checking help, it should be /V instead of -v. At least, that fixed it on Windows 7 :) Thanks for the article.

Mark W. Kaelin
Mark W. Kaelin

Do you find yourself adding menu items via registry edit often?

mudpuppy1
mudpuppy1

The 'Compress old files' option is not there in my version of Win 7 x64 (Enterprise), nor is it present on my W2K8 R2 system. Can't speak to the other versions at the moment so maybe it was removed.

jantzman60
jantzman60

I have removed the "Compress Older Files" option from the disk cleanup process. Backup your registry and remove the following keys from this registry location: (This is for XP - not sure about Vista/Windows 7) "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Compress old files" @="{B50F5260-0C21-11D2-AB56-00A0C9082678}" "Priority"=dword:0000012c "StateFlags"=dword:00000000 Reboot in order for the changes to be effective This change also speeds up the cleanup disk process.

butchy07
butchy07

thanks, you're right, it worked.

Mark W. Kaelin
Mark W. Kaelin

Give it a few minutes - I thought I had done something wrong myself, but I saw that the hard drive light was going crazy - turns out it was defragging without a GUI to look at.

pseudopygrapha
pseudopygrapha

Assume, for the sake of argument, that the regedit process takes 5 minutes. Also assume that it saves you 2 seconds each time you defragment a drive (it saves a total of 2 actual clicks in XP). You will begin saving time after 150 defrag operations. (To defrag a drive without all this trouble, right-click and select properties. Select Tools tab and click defragment. It was an interesting regedit exercise, however.)

jmorris
jmorris

just to create a link on your desktop and start your clean up and defrag that way? That is how I set it up here for users. Works well and I don't have to do have to wade into the registry.

Mark W. Kaelin
Mark W. Kaelin

I used the -v on my Windows 7 test machine and it worked fine. Has anyone else had problems with that parameter? Is there a difference between ultimate, which is on my test PC, and Home?

seanferd
seanferd

But I haven't run a fresh install recently, and some of the things I used to edit are now provided by small extensions or registry scripts, or reg/inf files I have created for the purpose. I suppose the next time I move to a new Windows OS permanently, the adventure will begin again. :)

Who Am I Really
Who Am I Really

and thanks for posting it, but it wasn't until after several GB of media files were corrupted that we went looking for it, I made the owner of the IT management company that handle our systems, come in and find it and remove it because of the lost / damaged files. I wish there was a GPO for removing the disk cleanup wizard like the a GPO for removing the desktop cleanup wizard Located here in XP: GPEdit.msc - navigate through User Configuration > Administrative Templates > Desktop > Remove the Desktop Cleanup Wizard (change setting to "Enabled")

CharlieSpencer
CharlieSpencer

I'm going to view the article as a general tutorial for modifying the right-click menu. I hope I'm not defragging my system often enough to require a right-click menu link. On systems I support, -if- I felt a fast way to access these options was necessary, I'd create desktop shortcuts; faster and no registry manipulation needed.