Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Add Command Verbs to the Win7 Explorer Context Menu

Published on
13,278 Points
1 Endorsement
Last Modified:
"Copy as path" is a one of the hidden context menu selections supported by the Windows 7 Explorer.  It appears on the menu when right-click while holding down the SHIFT key.  This article shows how to put that option on the regular (unshifted) context menu, and it provides example program code for adding your own commands to the Win7 Explorer context menu.
Right-Click to see "Copy as Path"How is Copy as Path, useful?  If you need to ask, you probably don't need it.  I use it a dozen time a day in writing scripts, managing websites, reorganizing projects... you name it.  With Win7, it is particularly useful because there are so many virtual folders, with names like Documents or Library\Music\Abby Road that are actually views onto an underlying physical directory on the hard disk.  Likewise, shared network directories have "real" names -- UNCs -- like \\ZEUS\Archive\Reports\2009-09 that are often lengthy.  It just turns out that a power user needs to copy-and-paste long file names often when scripting or doing maintenance.

In WinXP, it was possible to add "action verbs" to context menus via the Folder Options menu command.  Win7 has replaced this with the less dangerous and more user-friendly "Default Programs" Control Panel applet.  But, alas, it provides no way to add a new verb to any Explorer menu.  

However, the system registry handling for this facet of windows programming is basically the same.  Just add a new key at the right place in the registry, and you can add some handy new features to the Explorer.  And guess what?  You can implement the command functionality in JavaScript, VBScript, or even using DOS batch file commands.
[step=""]Note: Modifying the registry can be dangerous.  You should make a backup.[/step]
Add a new Action Verb
Follow these steps to add a new right-click context menu verb for any file type:

1) Open RegEdit and look in HKEY_CLASSES_ROOT for the file extension.  For instance, .XYZ

2) It will have as its (Default) value, another key, for instance XYZfile  Scroll down until you find that.

3) If XYZfile does not have any keys, add one named shell

4) Under HKCR\XYZfile\shell, add a key with the text of the command you want to show in the context menu.  For instance, Process with MyProg

5) Under HKCR\XYZfile\shell\Process with MyProg, add a new key, command

6) Set the (Default) value of HKCR\XYZfile\shell\Process with MyProg\command to the command you want to run.
RegEdit,  adding an Explorer context-menu commandYour command will typically include %1 at the end, so that the pathname of the file on which you right-clicked will be passed as a command-line argument to your program.

Add the Copy as Path Verb
This is the kind of command that you want to be able to apply to any file or folder.  So look for the key,
and follow the above steps.  The %1 that is passed to your program will be the text of the item (file, folder, or virtual folder) that you right-clicked in Explorer.

I'll provide a .REG file to simplify this for you.  But you really should become comfortable with the use of RegEdit if you intend to explore this part of shell programming.

The Program Code
Once you know how to add a verb to the Explorer context menu, you need to make that verb do what you want it to do.  In this case, we need to take a command-line parameter and put it onto the clipboard.  To keep it simple, we need to do it in a scripting language.  

I found two equally-reasonable ways to accomplish that:
1) Use the Clip.Exe command that comes with Vista and Win7, ...or...
2) Run an HTA (Hypertext Application) that provides access to the HTML DOM object, window.clipboardData

I tried several other options; for instance, you can instantiate an Internet Explorer, but using its clipboard functions trigger a security prompt.  You could also use Microsoft Word automation, which provides a clipboard object, but not everybody has Ms Word.  It's a piece of cake to write C++ or VB program to manipulate the clipboard, but I was aiming for a compiler-less solution.

Option #1: Using Clip.Exe
Clip.Exe is a command-line utility that comes standard with Win7.  It does one thing:  It reads from the standard input and places the data into the clipboard.  For instance:
DIR | Clip

Open in new window

...pipes the output of the DIR command into the clipboard.  Thus, all we need our context-menu handler to do is run a batch file that pushes its input parameter into the pipe.  The ECHO command can do that.  So, here's the code:

REM -- Used in the "Copy Path Name" context menu
echo "%1"| clip.Exe

Open in new window

[/step]And here's the .reg file that installs the context menu:
[step=""]CopyAsPath.REG (.BAT version)
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\Copy Path Name\command]
@="Cmd.Exe /c C:\\ProgramData\\MyScripts\\CopyAsPath.bat \"%1\""

Open in new window


Option #2: Using an HTA
A Hypertext Application Program (HTA) is able to access the clipboard via its window object.  Here's the code for an HTA that reads and parses its command line and then feeds its filename parameter into the clipboard.
<<!-- CopyAsPath.HTA puts command-line argument on the clipboard -->
   APPLICATIONNAME="Copying to Clipboard..."
   WINDOWSTATE = "minimize"
var sCmdLine= oHTA.commandLine;
//----- find the start of the arguments
var nOffset= sCmdLine.indexOf('"', 2 );
var sArgs= sCmdLine.substr( nOffset+1 );
while( sArgs.charAt(0) == ' ' ) {
   sArgs= sArgs.substr(1);
window.clipboardData.setData( 'Text', sArgs );

Open in new window

And here's the .REG file that installs the context menu for the HTA version:
[step=""]CopyAsPath.REG (.HTA version)
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\Copy Path Name\command]
@="mshta.exe \"C:\\ProgramData\\MyScripts\\CopyAsPath.HTA\"  \"%1\""

Open in new window

The .BAT version causes a brief flash as the DOS window opens and closes, while the .HTA version does not.  I personally like the .HTA version because it is very easily modified.  For instance, you can decide whether or not to put quotes around the clipboard data.  You could write a version of it that would copy just the filename (not the full path), or you could see if the argument is a .LNK file and provide some additional logic to track down the target program.

In addition to .BAT, and .HTA you can run .VBS and .JS files for your context menu handlers.  For these latter two options, you need to set Wscript.Exe as the executable, passing the name of the script file in the command line.
[step=""]NOTE: The included .REG files assume that the command handler scripts are in a directory named
I chose that location because it can be read and written without administrator privilege.  If you use the .REG files (or, any time you use RegEdit), you will be prompted to OK administrator access, but you can use normal user access to modifying the .BAT or .HTA files.[/step]

The Windows 7 Default Programs handling is easier to use than the old Folder Options/File Types dialog and it probably prevents some of tech-support headaches.  But it is missing the ability to add new context-menu verbs.

In this article, we looked at how to modify the system registry directly to add new commands to the Windows Explorer right-click context menu.  The example code provided the functionality of the "Copy as Path" -- something that already exists in Win7 (if you remember to press SHIFT :-) -- but most importantly, we explored the mechanisms involved.  With what you learned here, you can now add any command to the context menu using simple batch and script programs.

If you liked this article and want to see more from this author, please click the Yes button near the:
      Was this article helpful?
label that is just below and to the right of this text.   Thanks!
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Join & Write a Comment

The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month