151

I have found out how to add right-click context menu items to files on Windows Explorer, by adding keys to the registry. I.e. I can right-click on a file in Explorer and run a custom app against that file.

I would like to do the same for a folder and have not found a way to do that (yet). I see articles on creating/writing custom context menu handlers, but I would rather not go there.

I have found an article here on how to add cascading context menu items to the Desktop and to the "Computer" in Explorer, but this does not work for any folder.

I would like to be able to add my custom app to the context menu and have it work on both files and folders. Is there a way to do this without writing a context menu handler?


I found the solution in the below article, which describes how to do this via the registry for files, as well as for folders:

The following two articles provided additional info and options:

Revious
  • 7,816
  • 31
  • 98
  • 147
Elan
  • 6,084
  • 12
  • 64
  • 84

5 Answers5

301

In the registry editor (regedit.exe) find:

Right click on BACKGROUND of right panel

  1. HKEY_CLASSES_ROOT\Directory\Background\shell if you are administrator
  2. HKEY_CURRENT_USER\Software\Classes\directory\Background\shell if you are a normal user

Right click on FOLDER ICON in left/right panel of Windows Explorer

  1. HKEY_CLASSES_ROOT\Directory\shell if you are administrator
  2. HKEY_CURRENT_USER\Software\Classes\directory\shell if you are a normal user

Right click on file

  1. HKEY_CLASSES_ROOT\*\shell if you are administrator
  2. HKEY_CURRENT_USER\Software\Classes\*\shell if you are a normal user

screenshots

In all cases:

  1. add a new key under shell, naming it as you want to name the context menu item
  2. add a new key inside this key, named command (mandatory name)
  3. edit the default property in command to myprogrampath\path\path\executable.exe "%1" to pass the file path and name of the selected file to your custom program (for .../Directory/Background and .../directory/Background cases use %V instead of %1)

More customization:

  • Add icon: add a string value named icon for key created at step 1 with value matching an icon resource path. You can also provide an integer arguments to specify which icon to use. Example: %SystemRoot%\System32\shell32.dll,3
  • Display only on shift-click: add an empty string value named Extended for key created at step 1
  • Customize menu entry label: change the value of default value for key created at step 1
  • Change menu entry location: add a string value named Position with one of: Top, Bottom
jumpjack
  • 841
  • 1
  • 11
  • 17
  • 6
    `HKEY_CLASSES_ROOT\Directory\Background\shell` can only affect right click on background of a directory – Amos Sep 23 '15 at 12:59
  • 28
    Should be `"%1"` in case path has spaces. – H.v.M. Jan 13 '16 at 19:18
  • 1
    When are these supposed to take effect? Do they require a restart? – Michael Feb 29 '16 at 16:07
  • 7
    immediately. There isn't even need to open a new explorer window: new settings are loaded as soon as you right-click. – jumpjack Mar 01 '16 at 18:43
  • 1
    seems environment variables do not work in `Command`, do they? – Jack Lu Mar 25 '16 at 14:47
  • Yes, they're usually temporary and local to cmd windows. There was a specific panel of options to permanently add/edit env variable in winxp and win8. – jumpjack Mar 26 '16 at 19:34
  • Thanks. This is so helpful for me. It was hard to find a good explained answer on the internet. – Ali Tor Aug 22 '16 at 22:33
  • 1
    When I add the `%1`, I get an error dialog `This file does not have a program associated with it for performing this action. Please install a program or, if one is already installed, create an association in the Default Programs control panel.` Removing the `%1` gets rid of the error dialog and launches my app, but does not pass any arguments to my app. Any advice? – Michael Herrmann Aug 23 '17 at 10:45
  • 17
    Regarding my previous comment: The problem can be fixed by replacing `%1` by `%V`. – Michael Herrmann Aug 23 '17 at 10:48
  • @jumpjack This did not appear to be the case for me. I had to close my explorer window and open a new one. – Jack M Dec 17 '17 at 17:56
  • What if I select multiple files? How do I change(add to) the list then? `shell` doesn't work. – akinuri Feb 25 '19 at 08:18
  • @akinuri I don't understand the question – jumpjack Feb 26 '19 at 15:49
  • Microsoft itself has pretty good documentation with a lot of examples https://learn.microsoft.com/en-us/windows/win32/shell/context-menu-handlers – Bojan P. May 30 '20 at 10:12
  • @MichaelHerrmann Besides '%V', adding '.' also helps if you are dealing with a directory. – sisisisi Jun 05 '20 at 07:04
  • I was having trouble getting the icon to work until I saw this tip "icon & position string keys are not created in command, but its parent: whatever you called your app key in `\shell`" at https://stackoverflow.com/a/27090678/470749 – Ryan Jun 23 '20 at 01:21
  • In administrator mode: I prefer setting keys to `HKEY_LOCAL_MACHINE\Software\Classes\*` instead of `HKEY_CLASSES_ROOT\*` (same for `Directory` etc.). The effect is the same but it's more obvious what's happening, IMHO. – Zerte Sep 30 '20 at 08:06
  • I'm trying to figure out how to have an item appear in the context menu when you right click within an Explorer window, so within a directory and not on a specific file or directory. Does anyone have any advice? – Michael Murphy Jan 23 '21 at 17:29
  • The answer is updated corresponding to comments about quotes around `%1` and `%V` for `.../Directory/Background` and `.../directory/Background` cases – Nick Vee May 07 '21 at 12:56
  • Unfortunately, in Windows 11, adding to HKEY_CURRENT_USER\Software\Classes doesn't work: after you selected the added menu item, all changes are automatically removed. I was experimenting with it, and for specific program the item stayed after some manipulations. I cannot reproduce it for similar program. Adding to HKEY_CLASSES_ROOT worked. – Andrei Kalantarian Nov 14 '21 at 16:26
  • Also, anything related to file context menu, may not work even on Windows 10 after some update. I can reliably add the "double-click" action, changing the "UserChoice" registry key, but that is not what I was looking for. Preferable way at "double-click" would be to open the dialog, asking, which program to use, with your choice at the top. For some reason, Microsoft made a big secret around it. – Andrei Kalantarian Nov 14 '21 at 16:36
  • The registry key you wrote for the right panel is the key for the left panel and viceversa. – Canovas Jun 06 '23 at 00:04
  • @Canovas It's a little more complicated, added screenshot feom Windows 11 – jumpjack Jun 07 '23 at 07:40
  • Adding an & marks the following character to be used as keyboard shortcut (But it doesn't seem to work when inserted as the first character.) – Ryugeist Jul 04 '23 at 11:48
  • Following what @Amos said: `HKEY_CLASSES_ROOT\Directory\Background\shell` works for background of a directory. If you navigate to a directory via Library, this does not count as a directory, but as a library. Use `HKEY_CLASSES_ROOT\LibraryFolder\background\shell` in that case. – Ryugeist Jul 04 '23 at 11:56
16

Found a cleaner, easier and faster solution: create a text file, fill it with these contents, update it to your needs, save with .reg suffix and launch it (it does not need administrator priviliges because it accesses user-part of the registry):

Windows Registry Editor Version 5.00

; Setup context menu item for click on right panel:
[HKEY_CURRENT_USER\Software\Classes\directory\Background\shell\MenuItemNameBackground\command]
@="C:\\yourpath\\executable.exe \"%1\""

; Optional: specify an icon for the item:   
; HKEY_CURRENT_USER\Software\Classes\directory\Background\shell\MenuItemNameBackground]
;"icon"="C:\\yourpath\\appicon.ico"

; Optional: specify a position in the menu
; HKEY_CURRENT_USER\Software\Classes\directory\Background\shell\MenuItemNameBackground]
;"position"="Bottom"

; -------------------------------------------------------------------------------------

; Setup context menu item for click on folders tree item:
[HKEY_CURRENT_USER\Software\Classes\directory\shell\MenuItemNamePanel\command]
@="C:\\yourpath\\executable.exe \"%1\""

; Optional: specify an icon for the item:   
; [HKEY_CURRENT_USER\Software\Classes\directory\shell\MenuItemNamePanel]
;"icon"="C:\\yourpath\\appicon.ico"

; Optional: specify a position in the menu
; [HKEY_CURRENT_USER\Software\Classes\directory\shell\MenuItemNamePanel]
;"position"="Top"

In this way you can also have a backup of your configuration: just save the .reg file in a safe place. If you manually edit the registry after launching the file, right-click and slect "export".

Beware of double backspaces in path: \\

jumpjack
  • 841
  • 1
  • 11
  • 17
11

I went back and also answered this in another topic since there doesn't appear to be much on this question specifically.

I found the simplest way was to add a String Value to the key called "AppliesTo" and set its value to "under:{path}"

In my example, I want it to only look in the T Drive, so my String value is "AppliesTo":"under:T:".

In C#, this is easily accomplished with the following:

RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Folder\\Shell", true);
RegistryKey newkey = _key.CreateSubKey("My Menu Item");
newkey.SetValue("AppliesTo", "under:T:");

RegistryKey subNewkey = newkey.CreateSubKey("Command");
subNewkey.SetValue("", "C:\\yourApplication.exe");
subNewkey.Close();

newkey.Close();
_key.Close();
Community
  • 1
  • 1
Bobby Byrnes
  • 411
  • 5
  • 18
  • 2
    Thanks for providing sample code for doing this via C#. It's a small help for anyone who wants to do this quickly from an app. – Ayo I Apr 12 '17 at 19:09
  • 2
    Unfortunately, the "AppliesTo" value doesn't seem to have any effect on a "Directory\Background" entry. Does anyone know how similar limiting can be accomplished for right clicking on a folder's background? – cl0rkster Feb 22 '18 at 20:26
5

The only good solution I found a really working is : https://superuser.com/questions/1097054/shell-context-menu-registry-extension-doesnt-work-when-default-program-is-other

Add keys in HKEY_CLASSES_ROOT\SystemFileAssociations\your.extension\shell\command Modify the last key with the command you wanna do.

For my purpose it was :

"C:\Program Files (x86)\GPSBabel\gpsbabel.exe" -r -i gpx -f "%1" -x simplify,count=1000 -o gpx -F "%1.gpx"

If I export the it I get a .reg :

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\.gpx\shell\Simplify gpx\command]
@="\"C:\\Program Files (x86)\\GPSBabel\\gpsbabel.exe\" -r -i gpx -f \"%1\" -x simplify,count=1000 -o gpx -F \"%1.gpx\""
Dorian Grv
  • 421
  • 5
  • 9
1

Open command prompt [run as administrator] and execute this command

reg add "HKEY_CLASSES_ROOT\Directory\shell\Refi2\command" /d "powershell.exe -noexit -command Set-Location -literalPath '%V'"
  • -d : value to execute[app name exe].
  • -v : creates a new subkey inside the command key.
  • -f : to forcefully override the key if already exists.
  • powershell.exe -noexit -command Set-Location -literalPath '%V' instead of this you can specify path of your exe.

For more details about more features run:-

reg add /?
Sahil Shikalgar
  • 761
  • 8
  • 20