How to Add a Custom Entry to the Right-Click Context Menu in Batch Script
The Windows right-click context menu is one of the most powerful shortcuts available to any user. Adding your own custom entries to it can save significant time, whether you want to open a specific application, run a script, or copy the path of a file. In this guide, we will learn how to use a Batch Script to modify the Windows Registry and add custom context menu entries programmatically.
This technique is highly practical for system administrators, power users, and developers who want to streamline their workflows or distribute custom tools to a team.
How Context Menus Work in Windows
The right-click context menu is driven by entries stored in the Windows Registry. Different registry keys control different types of context menus:
HKEY_CLASSES_ROOT\*\shell: Appears when you right-click on any file.HKEY_CLASSES_ROOT\Directory\shell: Appears when you right-click on a folder.HKEY_CLASSES_ROOT\Directory\Background\shell: Appears when you right-click on the background of a folder (empty space inside a directory).HKEY_CLASSES_ROOT\Drive\shell: Appears when you right-click on a drive (like C: or D:).
Each entry under shell has a sub-key called command that specifies what executable or script to run when the menu item is clicked.
Prerequisites
Before proceeding, you should understand two things:
- Admin Privileges: Modifying
HKEY_CLASSES_ROOTrequires administrator rights. Your script must be run as an administrator. - The
REG ADDCommand: This is the built-in Windows command for adding registry keys and values from the command line.
Editing the Windows Registry can have serious consequences if done incorrectly. Always back up the registry before making changes. You can export a backup with: reg export HKCR\Directory\shell backup.reg.
Basic Syntax of REG ADD
The general form of the command is:
REG ADD "KeyPath" /v "ValueName" /t REG_SZ /d "Data" /f
/v: The name of the value being created./t REG_SZ: The data type (string)./d: The actual data to store./f: Forces the overwrite without prompting for confirmation.
Example 1: Adding "Open Notepad Here" to Folder Background
This is one of the most common custom entries. When you right-click on the empty space inside a folder, this will add an option to open Notepad.
@echo off
echo Adding "Open Notepad Here" to context menu...
REG ADD "HKCR\Directory\Background\shell\OpenNotepad" /ve /d "Open Notepad Here" /f
REG ADD "HKCR\Directory\Background\shell\OpenNotepad\command" /ve /d "notepad.exe" /f
echo Done. Restart Explorer or open a new folder to see the change.
pause
Breakdown
- The first
REG ADDcreates the menu item label "Open Notepad Here" under theshellkey. - The second
REG ADDcreates thecommandsub-key that tells Windows what to execute. /vesets the default value of the key (the unnamed value).
Example 2: Adding "Open CMD Here" to Folder Context Menu
This adds the option when you right-click directly on a folder.
@echo off
:: Require Administrator rights for HKCR
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrative privileges are required.
echo Please right-click this script and select "Run as administrator".
pause
exit /b 1
)
echo Adding "Open CMD Here" to folder context menus...
:: 1. Add to folder background (right-clicking empty space inside a folder)
REG ADD "HKCR\Directory\Background\shell\OpenCMDHere" /ve /d "Open CMD Here" /f >nul
REG ADD "HKCR\Directory\Background\shell\OpenCMDHere\command" /ve /d "cmd.exe /s /k pushd \"%%V\"" /f >nul
:: 2. Add to folder icon (right-clicking directly on a folder item)
REG ADD "HKCR\Directory\shell\OpenCMDHere" /ve /d "Open CMD Here" /f >nul
REG ADD "HKCR\Directory\shell\OpenCMDHere\command" /ve /d "cmd.exe /s /k pushd \"%%V\"" /f >nul
echo Complete.
pause
Understanding %%1
%1(or%%1inside a script) is a placeholder that Windows replaces with the path of the folder you right-clicked on.cmd.exe /k cd /d \"%%1\"opens a command prompt and changes the directory to the selected folder. The/kflag keeps the window open. The/dflag oncdensures the drive letter is also changed if the target folder is on a different drive.
If you are running this command directly in the console (not a .bat file), use %1 with a single percent sign. The double %% is only needed inside Batch scripts.
Example 3: Adding a Custom Script to File Context Menu
You can also run your own Batch script when a user right-clicks on any file. This is useful for tasks like "Copy file path to clipboard" or "Send file to backup."
@echo off
echo Adding "Copy File Path" to file context menu...
REG ADD "HKCR\*\shell\CopyPath" /ve /d "Copy File Path" /f
REG ADD "HKCR\*\shell\CopyPath" /v "Icon" /d "shell32.dll,134" /f
REG ADD "HKCR\*\shell\CopyPath\command" /ve /d "cmd.exe /c echo \"%%1\"| clip" /f
echo Done.
pause
Adding an Icon
The line REG ADD ... /v "Icon" /d "shell32.dll,134" /f assigns an icon from the Windows shell icon library. The number 134 corresponds to a clipboard-like icon. You can explore other icon indices by running rundll32 shell32.dll,Control_RunDLL.
Removing a Context Menu Entry
If you need to remove an entry you previously added, use REG DELETE.
@echo off
echo Removing "Open Notepad Here" from context menu...
REG DELETE "HKCR\Directory\Background\shell\OpenNotepad" /f
echo Entry removed.
pause
Always provide a removal script alongside your addition script, especially when distributing tools to other users. This gives them a clean way to undo the changes.
Common Mistakes
The Wrong Way: Forgetting Admin Rights
:: WRONG - no admin check
REG ADD "HKCR\*\shell\MyTool" /ve /d "My Tool" /f
Output Concern: The script will fail silently or display "Access is denied." without providing any context to the user.
The Correct Way: Checking for Admin at Script Start
@echo off
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
echo Right-click the script and select "Run as administrator".
pause
exit /b 1
)
echo Running with admin privileges...
REM Now safe to add registry keys
The net session command fails if the current session doesn't have admin rights, giving us a reliable way to check.
Complete Context Menu Installer Script
This comprehensive script provides a menu-driven interface for adding and removing multiple context menu entries.
@echo off
title Context Menu Customizer
setlocal enabledelayedexpansion
:: Admin check
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)
:menu
cls
color 0F
echo ===========================================
echo CONTEXT MENU CUSTOMIZER
echo ===========================================
echo.
echo [1] Add "Open CMD Here" (folder ^& background)
echo [2] Add "Copy File Path" (any file)
echo [3] Add "Open PowerShell Here" (folder ^& background)
echo [4] Remove "Open CMD Here"
echo [5] Remove "Copy File Path"
echo [6] Remove "Open PowerShell Here"
echo [7] Exit
echo.
set "opt="
set /p "opt=Select option: "
if "!opt!"=="1" (
REG ADD "HKCR\Directory\Background\shell\OpenCMDHere" /ve /d "Open CMD Here" /f >nul
REG ADD "HKCR\Directory\Background\shell\OpenCMDHere\command" /ve /d "cmd.exe /s /k pushd \"%%V\"" /f >nul
REG ADD "HKCR\Directory\shell\OpenCMDHere" /ve /d "Open CMD Here" /f >nul
REG ADD "HKCR\Directory\shell\OpenCMDHere\command" /ve /d "cmd.exe /s /k pushd \"%%V\"" /f >nul
echo [OK] "Open CMD Here" added [folder and background].
pause
goto menu
)
if "!opt!"=="2" (
REG ADD "HKCR\*\shell\CopyPath" /ve /d "Copy File Path" /f >nul
REG ADD "HKCR\*\shell\CopyPath" /v "Icon" /d "shell32.dll,134" /f >nul
REG ADD "HKCR\*\shell\CopyPath\command" /ve /d "cmd.exe /c echo \"%%1\"| clip" /f >nul
echo [OK] "Copy File Path" added.
pause
goto menu
)
if "!opt!"=="3" (
REG ADD "HKCR\Directory\Background\shell\OpenPSHere" /ve /d "Open PowerShell Here" /f >nul
REG ADD "HKCR\Directory\Background\shell\OpenPSHere\command" /ve /d "powershell.exe -NoExit -Command Set-Location -LiteralPath '%%V'" /f >nul
REG ADD "HKCR\Directory\shell\OpenPSHere" /ve /d "Open PowerShell Here" /f >nul
REG ADD "HKCR\Directory\shell\OpenPSHere\command" /ve /d "powershell.exe -NoExit -Command Set-Location -LiteralPath '%%V'" /f >nul
echo [OK] "Open PowerShell Here" added [folder and background].
pause
goto menu
)
if "!opt!"=="4" (
REG DELETE "HKCR\Directory\Background\shell\OpenCMDHere" /f >nul 2>&1
REG DELETE "HKCR\Directory\shell\OpenCMDHere" /f >nul 2>&1
echo [OK] Removed completely.
pause
goto menu
)
if "!opt!"=="5" (
REG DELETE "HKCR\*\shell\CopyPath" /f >nul 2>&1
echo [OK] Removed.
pause
goto menu
)
if "!opt!"=="6" (
REG DELETE "HKCR\Directory\Background\shell\OpenPSHere" /f >nul 2>&1
REG DELETE "HKCR\Directory\shell\OpenPSHere" /f >nul 2>&1
echo [OK] Removed completely.
pause
goto menu
)
if "!opt!"=="7" exit /b
goto menu
Explaining %V vs %1
%1: Contains the path of the item the user right-clicked on (a file or folder).%V: Contains the path of the folder in the background context (when right-clicking on empty space). This is the correct variable to use forDirectory\Background\shellentries.
Best Practices
- Always check for admin rights at the start of a registry-modifying script.
- Use
HKCRas a shorthand forHKEY_CLASSES_ROOTinREG ADDcommands. - Provide an uninstall option for every entry you create.
- Test with a single entry first before deploying a batch of context menu modifications.
- Back up the registry before running any script that modifies it.
Conclusion
Adding custom entries to the Windows right-click context menu using Batch Script is a practical and powerful technique.
By understanding the registry structure and using REG ADD, you can create personalized shortcuts that integrate seamlessly into the Windows workflow.
From opening terminals to copying paths, the possibilities are limited only by your imagination.