Add Defragment and Disk Cleanup to the right-click menu in Windows Explorer

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 de-fragmenting 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.

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).

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.

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).

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).

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.

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).

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 run as 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?

This entry was posted in Technical Archive. Bookmark the permalink.