How-To Geek

How to Add a Defragment Option to the Right-Click Menu for a Drive in Windows

dcm_top

Modern versions of Windows defragment drives during regular maintenance schedules. But if you defragment manually—maybe you keep your PC turned off when not in use—you might appreciate a faster way to access the command.

If you’re using Windows Vista up through Windows 10, you probably don’t need to manually defragment your drives anymore. This especially holds true if you’re using a solid state drive (SSD), which not only need to avoid the excessive wear and tear brought on by the process, but don’t need defragmentation anyway. Windows automatically defragments drives that need it once per week—by default at 1:00 am on Wednesdays. (And it is smart enough not to defragment SSDs at all.)

This automatic maintenance, however, requires that your PC be turned on at that time or at least be able to wake up from sleep for the defrag to happen. If you shut your PC down when you’re not using it, you might need to defragment drives on your own once in a while. Instead of digging through tools to do it, why not add a defragment command right to the context menu you get when you right-click a drive in File Explorer?

Add Defragment to the Context Menu by Editing the Registry Manually

To add a defragment command to the context menu, you just need to make a couple of quick edits in the Windows Registry.

Standard warning: Registry Editor is a powerful tool and misusing it can render your system unstable or even inoperable. This is a pretty simple hack and as long as you stick to the instructions, you shouldn’t have any problems. That said, if you’ve never worked with it before, consider reading about how to use the Registry Editor before you get started. And definitely back up the Registry (and your computer!) before making changes.

Open the Registry Editor by hitting Start and typing “regedit.” Press Enter to open Registry Editor and give it permission to make changes to your PC.

own_1

In the Registry Editor, use the left sidebar to navigate to the following key:

HKEY_CLASSES_ROOT\Drive\shell

dcm_2

Next, you’ll create a new key inside the shell key. Right-click the shell key and choose New > Key. Name the new key “runas.”

dcm_3

Now, you’ll change the (Default) value inside the new runas key. With the runas key selected, double-click the (Default) value to open its properties window.

dcm_4

In the properties window, set the value in the “Value data” box to “Defragment” and then click “OK.” This gives the command the name that will appear on the context menu.

dcm_5

Optionally, you can also set the command so that it only appears if you hold Shift down while right-clicking a drive—much the same way that the “Open Command Prompt” command is hidden unless you Shift+right-click a folder. To to that, right-click the runas key and choose New > String Value. Name the new value “Extended.” You don’t need to make any changes to it. Just having that string there will cause the command to be hidden behind Shift key access.

dcm_6

Whether you took the optional step of creating the Extended value or not, the rest of the process is the same. You’ll next need to create a new key inside your runas key. Right-click the runas key and choose New > Key. Name the new key “command.”

dcm_7

Now, you’ll change the (Default) value inside the new command key. With the command key selected, double-click the (Default) value to open its properties window.

dcm_8

The (Default) value specifies the actual command that will run when you select the option on the context menu. For our example, we’re going to call the command prompt defrag command and have it run with the default options, but with the verbose switch on so you can see the output of the command. To do that, type the following text into the “Value data” box and then click “OK.”.

defrag %1 -v

dcm_9

Since we’re calling the command prompt defrag command, you can also use any of the switches that command supports if you prefer. We’ve got a great guide that digs deeper into the defrag command and covers those additional options.

The changes should take place immediately, so you can exit out of Registry Editor. To test it out, just right-click (or Shift+right-click if you set up that option) any drive  and make sure the “Defragment” command is there.

dcm_10

When you run the command—which can take some time—you should see a command prompt window with the results.

dcm_11

If you want to reverse the changes at any time, just go back into the Registry and delete the runas key that you created. This will automatically delete any values and other keys you created inside the runas key and remove the command from your context menu.

Download Our One-Click Registry Hacks

dcm_12

If you don’t feel like diving into the Registry yourself, we’ve created some registry hacks you can use. The “Add Defrag to Context Menu” hack adds the defragment command to the regular context menu. The “Add Defrag to Shift Context Menu” adds the defragment command to the context menu you get when you use Shift+right-click. And the “Remove Defrag from Context Menu” removes the command no matter which way you added it. All three hacks are included in the following ZIP file. Double-click the one you want to use and click through the prompts.

Defrag Context Menu Hacks

These hacks are really just the runas key, stripped down to the additional keys and values we talked about in the previous section and then exported to a .REG file. Running the hacks just modifies the value. And if you enjoy fiddling with the Registry, it’s worth taking the time to learn how to make your own Registry hacks.

Walter Glenn is a long time computer geek and tech writer. Though he's mostly a Windows and gadget guy, he has a fondness for anything tech. You can follow him on Facebook and Twitter.

  • Published 12/6/16

More Articles You Might Like