How-To Geek
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.
Got Feedback? Join the discussion at discuss.howtogeek.com
Comments (16)
Programmer by day, geek by night, The Geek, also known as Lowell Heddings, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on Google+ if you'd like.
- Published 06/25/08




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
Nice! I have added -u switch at the end of defrag %1 -v to show me progress. Thanks
Great Tip!!!
Btw heads up, when accessing this page i was hit with a popup virus/trojan, i task killed the window rather then hitting the “x” but still scanning, please check your website for malware…i love this site and would hate to see it get hit with anything big*(or small to the simple minded computer people)
Thanks
i wont lie, i looked at all 50 hacks, and wow i am way over my head in even reading this stuff and trying to understand it,lol. what i really need is a hack for when i put in my resumes to companies online that they will look the best and always stay at the top of the list so they constantly see mine,lol. i doubt its possible tho.