Add Defragment to the Right-Click Menu for a Drive
After writing the article last week about how to add Disk Cleanup to the context menu for a drive, I received several requests for how to add another menu item for Defrag instead. With a simple registry hack we can do just that.
After manually applying or downloading the hack, you’ll have a new item on the right-click menu for your drives…
Which will start up the command-line version of Disk Defragmenter (after accepting the UAC prompt)
Manual Registry Hack
Open up regedit.exe through the start menu search or run box, and then browse down to the following key:
HKEY_CLASSES_ROOT\Drive\shell
Create a new key under shell called “runas”, and then set the (Default) value to “Defragment”. If you want to hide this menu item behind the Shift key right-click menu, then add a new string called Extended with no value.
Next, you’ll need to create a key called “command” and set the default value to the following, which is the command to run defrag with the default options but show verbose output.
defrag %1 -v
You can alternately choose from one of the other defrag switches here if you’d like.
Downloadable Registry Hack
Simply download, extract, and double-click on either AddDefragToDriveMenu.reg (for the regular menu) or AddDefragToExtendedDriveMenu.reg (to hide behind the Shift key). There’s also an included removal script that will remove either one.
Daily Email Updates
You can get our how-to articles in your inbox each day for free. Just enter your name and email below:
| Similar Articles | Featured Wiki Articles |
| Latest Software Reviews | Quick Linux Tips |
| Geek Arcade | Popular Forum Threads |


As one of those that requested this tweak, I humbly thank you.
Adding the “runas” piece overcame the administrator requirement, and the “Extended” string is a nice touch (works for the aforementioned previous post on adding Disk Cleanup too!).
Overall, the kind of elegant solution I’ve come to expect from your site. Well done.
Glad I could help! =)
I sure wanted to try this but without the download.
Since I have only minimal registry work I was careful to do exactly as you had posted.
However, when I select defragmenet I get
“Which user account do you want to run this program.”
I then selected current user.
But then I get just a flash of the DOS screen and it disappears.
I have double checked all the registry entries and they look just like the ones you posted. Any idea what I can do to keep the DOS screen from disappearing?
BTW, I don’t have Vista, I am using XP 2nd Edition.
THANKS
BRIAN
@Brian
For XP, you should rename the runas key to something else, like just “defrag”. I need to update the post to explain the alternate XP instructions… thanks for mentioning.
Hey Geek,
This was really helpful
how do you get to know about such tweaks
You rock!
You rock! Thanks!
Could you please do one for Check Disk as well?
@Mike
Will get on that.
Does this work for network drives as well?
hey thnx alot for the helpful little tweaks
greatly appreciated
It’s a nice little trick and I would jump on it in a second if Vista did not employ automatic defrag every week. On the rare occasion when a person might need to defrag a specific disk, it is easily done from the control panel.
I successfully implemented this reg hack, but the CMD prompt closes afterwards (automatically). Is there a way to have the CMD prompt stay open so I can review the results?
Thanks Geek, not only for a great hack, but a great site.
bw