How add context menu item to Windows Explorer for folders

Learn how add context menu item to windows explorer for folders with practical examples, diagrams, and best practices. Covers contextmenu, explorer, windows-shell-extension-menu development techniq...

Adding Custom Context Menu Items to Windows Explorer for Folders

Illustration of a right-click context menu in Windows Explorer with a custom item highlighted.

Learn how to extend Windows Explorer's right-click context menu for folders by modifying the registry, enabling quick access to custom actions and applications.

Windows Explorer's context menu, accessible via a right-click, is a powerful tool for quick actions. While Windows provides many default options, you might often find yourself wishing for a custom entry to perform a specific task, like opening a folder in a particular application, running a script, or accessing a custom utility. This article will guide you through the process of adding your own context menu items specifically for folders, leveraging the Windows Registry.

Understanding the Windows Registry Structure for Context Menus

The Windows Registry is a hierarchical database that stores low-level settings for the operating system and applications. For context menu extensions, we'll primarily interact with the HKEY_CLASSES_ROOT hive. This hive contains information about registered applications, file types, and shell extensions. Specifically, folder context menu items are defined under the Directory\shell and Directory\Background\shell keys.

flowchart TD
    A["HKEY_CLASSES_ROOT"] --> B["Directory"]
    B --> C["shell"]
    C --> D["YourCustomMenuItem"]
    D --> E["command"]
    E --> F["PathToExecutable.exe \"%1\""]
    B --> G["Background"]
    G --> H["shell"]
    H --> I["YourCustomBackgroundMenuItem"]
    I --> J["command"]
    J --> K["PathToExecutable.exe \"%V\""]

Registry structure for adding custom folder context menu items

There are two main locations for adding folder context menu items:

  1. HKEY_CLASSES_ROOT\Directory\shell: Items added here will appear when you right-click directly on a folder icon.
  2. HKEY_CLASSES_ROOT\Directory\Background\shell: Items added here will appear when you right-click on the background of an open folder window (i.e., not on a file or subfolder). This is useful for actions that apply to the current directory itself.

Creating a Custom Context Menu Item for Folders

To add a custom context menu item, you'll need to create a new key under the appropriate shell key, and then a subkey named command. The default value of the command subkey will specify the executable or script to run, along with any parameters.

1. Open Registry Editor

Press Win + R, type regedit, and press Enter. Confirm the UAC prompt if it appears.

2. Navigate to the Target Key

For right-clicking on a folder icon, go to HKEY_CLASSES_ROOT\Directory\shell. For right-clicking on the background of a folder, go to HKEY_CLASSES_ROOT\Directory\Background\shell.

3. Create a New Key for Your Menu Item

Right-click on shell, select New > Key, and name it something descriptive, like OpenWithVSCode or RunCustomScript. This name will be the text displayed in the context menu.

4. Set the Display Name (Optional)

With your new key selected, in the right pane, double-click the (Default) value and set its data to the exact text you want to appear in the context menu (e.g., Open Folder in VS Code). If you skip this, the key name will be used.

5. Create the 'command' Subkey

Right-click on your newly created menu item key (e.g., OpenWithVSCode), select New > Key, and name it command.

6. Specify the Command to Execute

With the command key selected, double-click the (Default) value in the right pane. Set its data to the full path of the executable or script you want to run, followed by any arguments. Use "%1" for folder icons or "%V" for folder background to pass the selected folder's path. For example: "C:\Program Files\Microsoft VS Code\Code.exe" "%1".

7. Test Your New Menu Item

Close Registry Editor. Navigate to a folder in Windows Explorer, right-click on it (or its background, depending on where you added the key), and you should see your new context menu item. Click it to test its functionality.

Example: Opening a Folder in Visual Studio Code

Let's walk through a common use case: adding an option to open any folder directly in Visual Studio Code from the context menu.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\OpenWithVSCode]
@="Open Folder in VS Code"
"Icon"="C:\\Program Files\\Microsoft VS Code\\Code.exe,0"

[HKEY_CLASSES_ROOT\Directory\shell\OpenWithVSCode\command]
@="\"C:\\Program Files\\Microsoft VS Code\\Code.exe\" \"%1\""

Registry (.reg) file to add 'Open Folder in VS Code' to folder context menu.

To use this .reg file:

  1. Copy the content above into a plain text file.
  2. Save the file with a .reg extension (e.g., vscode_context.reg).
  3. Double-click the .reg file and confirm the prompts to merge it into your registry.

Note the "Icon" entry: this is optional but allows you to specify an icon for your menu item, making it more visually appealing. The ",0" indicates the first icon within the executable.

Advanced Options: Submenus and Conditional Visibility

For more complex scenarios, you can create submenus or make menu items appear only under certain conditions. This involves additional registry entries.

To create a submenu, instead of directly creating a command subkey under your menu item key, you create a shell subkey, and then add your individual commands under that shell key. For example:

HKEY_CLASSES_ROOT\Directory\shell\MyTools\shell\Tool1\command

Conditional visibility can be achieved using AppliesTo or Extended values. The AppliesTo value (a REG_SZ string) allows you to specify conditions based on file properties or folder names (e.g., AppliesTo="*.txt" or AppliesTo="SystemFolder"). The Extended value (an empty REG_SZ string) makes the menu item appear only when the Shift key is held down during the right-click.