How to Add "Open Command Prompt Here" to the Explorer Context Menu in Batch Script
Having a quick way to open a Command Prompt directly in the folder you are currently browsing is one of the most time-saving shortcuts a Windows user can have. While modern versions of Windows have added "Open in Terminal" options, many users still prefer the classic cmd.exe experience or need it for compatibility reasons. In this guide, we will create a Batch Script that adds "Open Command Prompt Here" to the Windows Explorer context menu by modifying the Windows Registry.
This approach is especially useful for system administrators deploying standard tools across multiple workstations or for developers who frequently work in the terminal.
Why You Might Need This
In recent versions of Windows 10 and Windows 11, Microsoft has replaced the traditional "Open command window here" option with "Open in Windows Terminal" or "Open PowerShell window here." While PowerShell is powerful, many scripts and legacy tools still depend on cmd.exe. Manually navigating with cd to a deeply nested folder wastes time.
By adding the entry yourself, you regain that quick access regardless of Windows version or Group Policy settings.
Understanding the Registry Keys
The context menu behavior is controlled by the Windows Registry. Two main locations are relevant:
HKEY_CLASSES_ROOT\Directory\shell: Controls what appears when you right-click on a folder.HKEY_CLASSES_ROOT\Directory\Background\shell: Controls what appears when you right-click on the empty background inside a folder window.
Each shell entry has:
- A default value that appears as the menu text.
- A
commandsub-key whose default value contains the executable to run.
There is also HKEY_CLASSES_ROOT\Drive\shell for adding entries to drive icons (C:, D:, etc.), and HKEY_CLASSES_ROOT\LibraryFolder\Background\shell for Windows Libraries.
The Simple Approach
Adding to Folder Background (Right-Click on Empty Space)
@echo off
echo Adding "Open Command Prompt Here" to folder background...
REG ADD "HKCR\Directory\Background\shell\cmd_here" /ve /d "Open Command Prompt Here" /f
REG ADD "HKCR\Directory\Background\shell\cmd_here" /v "Icon" /d "cmd.exe" /f
REG ADD "HKCR\Directory\Background\shell\cmd_here\command" /ve /d "cmd.exe /s /k pushd \"%%V\"" /f
echo Entry added successfully.
pause
Explaining Each Line
/ve /d "Open Command Prompt Here": Sets the default (unnamed) value of the key to the text displayed in the menu./v "Icon" /d "cmd.exe": Adds the Command Prompt icon next to the menu entry. Windows automatically extracts the icon from the executable.cmd.exe /s /k pushd "%%V": Opens CMD, changes the directory to%V(the current folder path), and keeps the window open (/k).
Adding to Folder Right-Click (Right-Click on a Folder)
@echo off
echo Adding "Open Command Prompt Here" to folder right-click...
REG ADD "HKCR\Directory\shell\cmd_here" /ve /d "Open Command Prompt Here" /f
REG ADD "HKCR\Directory\shell\cmd_here" /v "Icon" /d "cmd.exe" /f
REG ADD "HKCR\Directory\shell\cmd_here\command" /ve /d "cmd.exe /s /k pushd \"%%1\"" /f
echo Entry added successfully.
pause
Notice the difference: when right-clicking on a folder, the path is passed via %1, not %V.
Common Mistakes
The Wrong Way: Forgetting to Run as Admin
:: WRONG - no admin check
@echo off
REG ADD "HKCR\Directory\Background\shell\cmd_here" /ve /d "Open CMD Here" /f
Output:
ERROR: Access is denied.
The script fails because modifying HKEY_CLASSES_ROOT requires elevated privileges.
The Correct Way: Admin Check First
@echo off
:: Check for administrator rights
net session >nul 2>&1
if %errorlevel% neq 0 (
echo ============================================
echo ERROR: Administrator privileges required.
echo ============================================
echo.
echo Please right-click this script and select
echo "Run as administrator".
echo.
pause
exit /b 1
)
echo Running with admin rights. Proceeding...
This ensures a clear error message is displayed before any registry modification is attempted.
The Wrong Way: Using cd Instead of pushd
:: WRONG - cd does not work across drives
cmd.exe /k cd "%V"
Output Concern:
If you are browsing D:\Projects and the command prompt defaults to C:\, the cd command will not change to the D: drive. You'd still be on C:.
The Correct Way: Using pushd
cmd.exe /s /k pushd "%V"
The pushd command handles drive changes automatically. It creates a temporary network mapping for UNC paths as well, making it far more reliable than cd.
The Complete Installer and Uninstaller Script
This polished script provides options to install or remove the context menu entry for both folder and background contexts.
@echo off
title CMD Context Menu Installer
setlocal
:: --- Admin Check ---
net session >nul 2>&1
if %errorlevel% neq 0 (
color 0C
echo [ERROR] This script requires Administrator privileges.
echo Right-click the file and choose "Run as administrator."
pause
exit /b 1
)
:menu
cls
color 0F
echo =============================================
echo "Open Command Prompt Here" Installer
echo =============================================
echo.
echo [1] Install (Folder Background)
echo [2] Install (Folder Right-Click)
echo [3] Install Both
echo [4] Uninstall (Folder Background)
echo [5] Uninstall (Folder Right-Click)
echo [6] Uninstall Both
echo [7] Exit
echo.
set "opt="
set /p "opt=Select an option: "
if "%opt%"=="1" goto install_bg
if "%opt%"=="2" goto install_folder
if "%opt%"=="3" goto install_both
if "%opt%"=="4" goto uninstall_bg
if "%opt%"=="5" goto uninstall_folder
if "%opt%"=="6" goto uninstall_both
if "%opt%"=="7" exit /b
goto menu
:install_bg
echo.
echo Installing for folder background...
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere" /ve /d "Open Command Prompt Here" /f >nul
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere" /v "Icon" /d "cmd.exe" /f >nul
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere\command" /ve /d "cmd.exe /s /k pushd \"%%V\"" /f >nul
echo [OK] Background context menu entry added.
pause
goto menu
:install_folder
echo.
echo Installing for folder right-click...
REG ADD "HKCR\Directory\shell\OpenCmdHere" /ve /d "Open Command Prompt Here" /f >nul
REG ADD "HKCR\Directory\shell\OpenCmdHere" /v "Icon" /d "cmd.exe" /f >nul
REG ADD "HKCR\Directory\shell\OpenCmdHere\command" /ve /d "cmd.exe /s /k pushd \"%%1\"" /f >nul
echo [OK] Folder context menu entry added.
pause
goto menu
:install_both
echo.
echo Installing for both contexts...
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere" /ve /d "Open Command Prompt Here" /f >nul
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere" /v "Icon" /d "cmd.exe" /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 Command Prompt Here" /f >nul
REG ADD "HKCR\Directory\shell\OpenCmdHere" /v "Icon" /d "cmd.exe" /f >nul
REG ADD "HKCR\Directory\shell\OpenCmdHere\command" /ve /d "cmd.exe /s /k pushd \"%%1\"" /f >nul
echo [OK] Both context menu entries added.
pause
goto menu
:uninstall_bg
echo.
echo Removing background context menu entry...
REG DELETE "HKCR\Directory\Background\shell\OpenCmdHere" /f >nul 2>&1
echo [OK] Background entry removed.
pause
goto menu
:uninstall_folder
echo.
echo Removing folder context menu entry...
REG DELETE "HKCR\Directory\shell\OpenCmdHere" /f >nul 2>&1
echo [OK] Folder entry removed.
pause
goto menu
:uninstall_both
echo.
echo Removing both context menu entries...
REG DELETE "HKCR\Directory\Background\shell\OpenCmdHere" /f >nul 2>&1
REG DELETE "HKCR\Directory\shell\OpenCmdHere" /f >nul 2>&1
echo [OK] Both entries removed.
pause
goto menu
Advanced Customizations
Adding a Keyboard Shortcut
You can assign a keyboard shortcut (an accelerator key) to your context menu entry by adding an ampersand (&) before the letter in the menu text.
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere" /ve /d "Open &Command Prompt Here" /f
Now, when the context menu is open, pressing C on your keyboard will activate the entry.
Adding It Only When Shift is Held
To keep the context menu clean during normal use and only show the entry when Shift+Right-Click is used:
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere" /v "Extended" /t REG_SZ /d "" /f
The Extended value tells Windows to only display the entry in the "extended" context menu (triggered by Shift+Right-Click).
Changing the Position
By default, new entries appear in alphabetical order. To move your entry to a specific position:
:: Place at the top of the context menu
REG ADD "HKCR\Directory\Background\shell\OpenCmdHere" /v "Position" /d "Top" /f
Valid values are Top, Bottom, or omitted (default alphabetical sorting).
Combining Position=Top with a distinctive icon makes your custom entry immediately visible, which is great for frequently used tools.
Best Practices
- Always include an uninstaller: Never distribute a registry-modifying script without a way to reverse the changes.
- Use
pushdovercd: It handles cross-drive navigation and UNC paths gracefully. - Test on a single machine first: Verify the entry appears correctly and launches the correct application before deploying to multiple systems.
- Back up the registry: Run
reg export HKCR\Directory\shell backup.regbefore making changes. - Use
>nul: Suppress output fromREG ADDto keep the script's interface clean.
Conclusion
Adding "Open Command Prompt Here" to the Windows Explorer context menu is a small but impactful customization.
With a simple Batch Script, you can automate the registry changes, provide installation and removal options, and even customize the entry's icon, position, and visibility.
This technique is a practical example of how Batch scripting can directly enhance the Windows user experience.