Skip to main content

How to Remove a Custom Context Menu Entry in Batch Script

After adding custom entries to the Windows Explorer right-click context menu, there will come a time when you need to remove them. Whether the entry is outdated, was added by mistake, or is no longer needed, knowing how to cleanly remove it is just as important as knowing how to add it. In this guide, we will walk through the process of identifying and deleting custom context menu entries using Batch Script and the REG DELETE command.

This technique is essential for system administrators who maintain clean system configurations and for developers who provide installation and uninstallation scripts for their tools.

Where Context Menu Entries Live

Before removing an entry, you need to know where it is stored in the Windows Registry. The most common locations are:

Registry PathApplies To
HKCR\*\shellRight-click on any file
HKCR\Directory\shellRight-click on a folder
HKCR\Directory\Background\shellRight-click on empty space inside a folder
HKCR\Drive\shellRight-click on a drive

Each custom entry is a sub-key under shell, and it always has a command sub-key inside it. For example, an entry called "Open CMD Here" would be:

HKCR\Directory\Background\shell\OpenCmdHere
HKCR\Directory\Background\shell\OpenCmdHere\command

Finding Existing Custom Entries

Before deleting, you should verify that the entry actually exists. The REG QUERY command lets you inspect registry keys.

@echo off
echo Checking for custom context menu entries...
echo.

echo --- File Context Menu ---
REG QUERY "HKCR\*\shell" 2>nul
echo.

echo --- Folder Context Menu ---
REG QUERY "HKCR\Directory\shell" 2>nul
echo.

echo --- Folder Background Context Menu ---
REG QUERY "HKCR\Directory\Background\shell" 2>nul

pause

This will list all sub-keys under each shell location. Look for names that you recognize as custom additions (like OpenCmdHere, CopyPath, etc.).

info

Default Windows entries like open, explore, and find should not be deleted, as they are part of the standard operating system behavior.

Basic Removal Syntax

The core command for removing a registry key is:

REG DELETE "KeyPath" /f

The /f flag forces the deletion without a confirmation prompt. Without it, the user will be asked "Are you sure?" which is problematic in automated scripts.

Example: Removing a Single Entry

@echo off
echo Removing "Open CMD Here" from folder background...
REG DELETE "HKCR\Directory\Background\shell\OpenCmdHere" /f
echo Done.
pause

The REG DELETE command automatically removes the key and all its sub-keys (including the command sub-key), so you only need to target the parent key.

Common Mistakes

The Wrong Way: Deleting Only the Command Sub-Key

:: WRONG - only removes the command, not the menu entry
REG DELETE "HKCR\Directory\Background\shell\OpenCmdHere\command" /f

Output Concern: The menu entry label "Open CMD Here" will still appear in the context menu, but clicking it will do nothing (or produce an error). This is a broken state.

The Correct Way: Remove the Parent Key

:: CORRECT - removes the entry and its command
REG DELETE "HKCR\Directory\Background\shell\OpenCmdHere" /f

By deleting the parent key, both the display name and the associated command are removed cleanly.

The Wrong Way: No Admin Check

:: WRONG - will fail silently or with "Access denied"
@echo off
REG DELETE "HKCR\*\shell\CopyPath" /f

The Correct Way: Verify Permissions First

@echo off
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
echo Please run this script as administrator.
pause
exit /b 1
)

REG DELETE "HKCR\*\shell\CopyPath" /f
echo [OK] Entry removed.
pause

Checking If an Entry Exists Before Deleting

A robust script should verify the entry exists before attempting deletion. This prevents confusing error messages.

@echo off
set "key=HKCR\Directory\Background\shell\OpenCmdHere"

REG QUERY "%key%" >nul 2>&1
if %errorlevel% equ 0 (
echo Entry found. Removing...
REG DELETE "%key%" /f >nul
echo [OK] Successfully removed.
) else (
echo [INFO] Entry does not exist. Nothing to remove.
)
pause
tip

This pattern is especially useful when distributing uninstaller scripts. Users who never installed the entry in the first place will see a helpful message instead of a confusing error.

Removing Multiple Entries at Once

If you previously installed several context menu entries, you can remove them all in a single script.

@echo off
title Context Menu Cleanup
setlocal enabledelayedexpansion

:: Admin check
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin rights required.
pause
exit /b 1
)

echo =============================================
echo CONTEXT MENU CLEANUP TOOL
echo =============================================
echo.

:: Define entries to remove
set "entry1=HKCR\Directory\Background\shell\OpenCmdHere"
set "entry2=HKCR\*\shell\CopyPath"
set "entry3=HKCR\Directory\shell\OpenPSHere"
set "entry4=HKCR\*\shell\EditWithNotepad"

:: Process each entry
for %%k in ("%entry1%" "%entry2%" "%entry3%" "%entry4%") do (
REG QUERY %%k >nul 2>&1
if !errorlevel! equ 0 (
REG DELETE %%k /f >nul
echo [REMOVED] %%~k
) else (
echo [SKIPPED] %%~k (not found^)
)
)

echo.
echo Cleanup complete.
pause
warning

Double-check your key paths before running a bulk removal. A typo could target a legitimate Windows entry, potentially disrupting system functionality.

Removing Entries Added by Third-Party Software

Sometimes third-party applications add context menu entries that you want to remove. These may be stored in slightly different registry paths:

  • HKCR\*\shellex\ContextMenuHandlers (shell extensions)
  • HKCR\.ext\shell (file-type-specific entries, where .ext is a file extension)
  • HKCU\Software\Classes\*\shell (per-user entries)

For standard shell entries, the same REG DELETE approach works. However, shellex\ContextMenuHandlers entries are COM-based and require more careful handling. These are typically GUIDs (like {12345678-1234-1234-1234-123456789ABC}) and should only be removed if you are certain of what they control.

:: Example: Removing a known shell extension handler
REG DELETE "HKCR\*\shellex\ContextMenuHandlers\SomeApp" /f

Complete Removal Tool with Interactive Menu

@echo off
title Context Menu Entry Remover
setlocal enabledelayedexpansion

net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Run as administrator.
pause
exit /b 1
)

:menu
cls
color 0F
echo =============================================
echo CONTEXT MENU ENTRY REMOVER
echo =============================================
echo.
echo [1] List all file context menu entries
echo [2] List all folder context menu entries
echo [3] List all background context menu entries
echo [4] Remove a specific entry by name
echo [5] Exit
echo.
set "opt="
set /p "opt=Select: "

if "!opt!"=="1" (
echo.
echo --- Entries under HKCR\*\shell ---
REG QUERY "HKCR\*\shell" 2>nul
echo.
pause
goto menu
)
if "!opt!"=="2" (
echo.
echo --- Entries under HKCR\Directory\shell ---
REG QUERY "HKCR\Directory\shell" 2>nul
echo.
pause
goto menu
)
if "!opt!"=="3" (
echo.
echo --- Entries under HKCR\Directory\Background\shell ---
REG QUERY "HKCR\Directory\Background\shell" 2>nul
echo.
pause
goto menu
)
if "!opt!"=="4" (
echo.
echo Enter the full registry key path to delete.
echo Example: HKCR\Directory\shell\MyEntry
echo.
set "key_path="
set /p "key_path=Key Path: "

if not defined key_path (
echo [ERROR] No path entered.
pause
goto menu
)

REG QUERY "!key_path!" >nul 2>&1
if !errorlevel! equ 0 (
set "confirm="
set /p "confirm=Are you sure you want to delete '!key_path!'? (Y/N): "
if /i "!confirm!"=="Y" (
REG DELETE "!key_path!" /f >nul
echo [OK] Entry removed.
) else (
echo [CANCELLED] No changes made.
)
) else (
echo [NOT FOUND] The specified key does not exist.
)
pause
goto menu
)
if "!opt!"=="5" exit /b
goto menu

Best Practices

  1. Always delete the parent key, not just the command sub-key. This ensures the menu entry is fully removed.
  2. Check for existence first using REG QUERY to avoid unnecessary error messages.
  3. Require admin rights for any script that modifies HKEY_CLASSES_ROOT.
  4. Keep a record of entries you add so you know exactly what to remove later.
  5. Restart Explorer if the context menu doesn't update immediately. You can do this with taskkill /f /im explorer.exe & start explorer.exe.

Conclusion

Removing custom context menu entries in Batch Script is a straightforward process once you understand the registry structure. By using REG DELETE with proper key paths and admin privileges, you can maintain a clean and organized right-click menu. Whether you are performing routine cleanup or building an uninstaller for a distributed tool, these techniques ensure a professional and reversible approach to context menu management.