Skip to main content

How to Add a "Copy Path" Option to the Context Menu in Batch Script

Copying a file or folder's full path is something developers and system administrators do countless times a day. While Windows has introduced some native ways to do this (like Shift+Right-Click), they are often hidden or inconsistent across versions. In this guide, we will create a Batch Script that adds a clean, reliable "Copy Path" option to the right-click context menu, allowing you to copy any file or folder path to the clipboard with a single click.

This is one of the most practical registry modifications you can make, and it works across all modern versions of Windows.

How the "Copy Path" Feature Works

The mechanism is simple:

  1. A custom entry is added to the Windows Registry under the appropriate shell key.
  2. When the user right-clicks a file or folder, the custom option appears.
  3. Clicking it runs a small command that pipes the selected item's path into the Windows clipboard using the clip utility.

Understanding the clip Command

The clip command is a built-in Windows utility that takes input from the standard output (stdout) and places it on the system clipboard. It is the key ingredient in our solution.

:: Example: copy text to clipboard
echo Hello World | clip

After running this command, pressing Ctrl+V anywhere will paste "Hello World".

info

The clip command has been available since Windows Vista and works reliably in all subsequent versions, including Windows 10 and Windows 11.

Registry Locations for Different Targets

Depending on what you want the "Copy Path" option to apply to, you need to target different registry keys:

TargetRegistry Path
Any fileHKCR\*\shell
Folders (right-click on folder)HKCR\Directory\shell
Folder background (empty space)HKCR\Directory\Background\shell
DrivesHKCR\Drive\shell

Adding "Copy Path" for Files

This is the most common use case. When you right-click on any file, you will see a "Copy Path" option.

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

echo Adding "Copy Path" to file context menu...

REG ADD "HKCR\*\shell\CopyFilePath" /ve /d "Copy Path" /f
REG ADD "HKCR\*\shell\CopyFilePath" /v "Icon" /d "shell32.dll,134" /f
REG ADD "HKCR\*\shell\CopyFilePath\command" /ve /d "cmd.exe /c echo %%1 | clip" /f

echo [OK] Done. Right-click any file to see the new option.
pause

How It Works

  • %1 is replaced by Windows Explorer with the full path of the file you right-clicked on. Inside the batch script, %%1 is used so that one % is consumed by the batch parser, and %1 is written into the registry.
  • echo %1 | clip prints that path and pipes it into the clipboard.
  • cmd.exe /c runs the command and then closes the CMD window immediately.

Adding "Copy Path" for Folders

To copy the path when right-clicking directly on a folder:

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

echo Adding "Copy Path" to folder context menu...

REG ADD "HKCR\Directory\shell\CopyFolderPath" /ve /d "Copy Folder Path" /f
REG ADD "HKCR\Directory\shell\CopyFolderPath" /v "Icon" /d "shell32.dll,134" /f
REG ADD "HKCR\Directory\shell\CopyFolderPath\command" /ve /d "cmd.exe /c echo %%1 | clip" /f

echo [OK] Done.
pause

Adding "Copy Path" for Folder Background

When you are inside a folder and right-click on the empty background, this copies the current folder's path:

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

echo Adding "Copy Current Folder Path" to background context menu...

REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath" /ve /d "Copy Current Path" /f
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath" /v "Icon" /d "shell32.dll,134" /f
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath\command" /ve /d "cmd.exe /c echo %%V | clip" /f

echo [OK] Done.
pause

Notice the use of %%V instead of %%1. The %V variable represents the current directory path in background context menus.

Common Mistakes

The Wrong Way: Path with Spaces Not Quoted

:: WRONG - paths with spaces will be truncated
REG ADD "HKCR\*\shell\CopyPath\command" /ve /d "cmd.exe /c echo %1 | clip" /f

Output Concern: If the file path is C:\My Documents\file.txt, the clipboard will only contain C:\My because the space breaks the echo command. Additionally, using %1 instead of %%1 inside a batch file means the batch parser consumes the % sign and the literal %1 never reaches the registry.

The Correct Way: Wrapping in Quotes

REG ADD "HKCR\*\shell\CopyPath\command" /ve /d "cmd.exe /c echo \"%%1\" | clip" /f

By quoting %1, paths with spaces are preserved correctly. The clipboard will contain the full path including quotes, such as "C:\My Documents\file.txt". Some users prefer this format, while others may want the path without quotes.

Alternative: Path Without Quotes Using Parentheses

REG ADD "HKCR\*\shell\CopyPath\command" /ve /d "cmd.exe /c (echo %%1) | clip" /f

Using parentheses keeps the full path together while avoiding extra quoting.

tip

Test both approaches and choose the one that fits your workflow. If you frequently paste paths into command-line tools, quoted paths are generally safer.

Complete Installer and Uninstaller

This comprehensive script handles installation and removal for all three contexts (file, folder, background).

@echo off
title Copy Path Context Menu Manager
setlocal

:: 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 COPY PATH - CONTEXT MENU MANAGER
echo =============================================
echo.
echo --- Install ---
echo [1] Add "Copy Path" for files
echo [2] Add "Copy Path" for folders
echo [3] Add "Copy Current Path" for folder background
echo [4] Install All
echo.
echo --- Remove ---
echo [5] Remove "Copy Path" for files
echo [6] Remove "Copy Path" for folders
echo [7] Remove "Copy Current Path" for background
echo [8] Remove All
echo.
echo [0] Exit
echo.
set /p "opt=Select: "

if "%opt%"=="1" goto install_file
if "%opt%"=="2" goto install_folder
if "%opt%"=="3" goto install_background
if "%opt%"=="4" goto install_all
if "%opt%"=="5" goto remove_file
if "%opt%"=="6" goto remove_folder
if "%opt%"=="7" goto remove_background
if "%opt%"=="8" goto remove_all
if "%opt%"=="0" exit /b
goto menu

:install_file
REG ADD "HKCR\*\shell\CopyFilePath" /ve /d "Copy Path" /f >nul
REG ADD "HKCR\*\shell\CopyFilePath" /v "Icon" /d "shell32.dll,134" /f >nul
REG ADD "HKCR\*\shell\CopyFilePath\command" /ve /d "cmd.exe /c echo \"%%1\" | clip" /f >nul
echo [OK] File "Copy Path" installed.
pause
goto menu

:install_folder
REG ADD "HKCR\Directory\shell\CopyFolderPath" /ve /d "Copy Folder Path" /f >nul
REG ADD "HKCR\Directory\shell\CopyFolderPath" /v "Icon" /d "shell32.dll,134" /f >nul
REG ADD "HKCR\Directory\shell\CopyFolderPath\command" /ve /d "cmd.exe /c echo \"%%1\" | clip" /f >nul
echo [OK] Folder "Copy Path" installed.
pause
goto menu

:install_background
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath" /ve /d "Copy Current Path" /f >nul
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath" /v "Icon" /d "shell32.dll,134" /f >nul
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath\command" /ve /d "cmd.exe /c echo \"%%V\" | clip" /f >nul
echo [OK] Background "Copy Current Path" installed.
pause
goto menu

:install_all
REG ADD "HKCR\*\shell\CopyFilePath" /ve /d "Copy Path" /f >nul
REG ADD "HKCR\*\shell\CopyFilePath" /v "Icon" /d "shell32.dll,134" /f >nul
REG ADD "HKCR\*\shell\CopyFilePath\command" /ve /d "cmd.exe /c echo \"%%1\" | clip" /f >nul
REG ADD "HKCR\Directory\shell\CopyFolderPath" /ve /d "Copy Folder Path" /f >nul
REG ADD "HKCR\Directory\shell\CopyFolderPath" /v "Icon" /d "shell32.dll,134" /f >nul
REG ADD "HKCR\Directory\shell\CopyFolderPath\command" /ve /d "cmd.exe /c echo \"%%1\" | clip" /f >nul
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath" /ve /d "Copy Current Path" /f >nul
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath" /v "Icon" /d "shell32.dll,134" /f >nul
REG ADD "HKCR\Directory\Background\shell\CopyCurrentPath\command" /ve /d "cmd.exe /c echo \"%%V\" | clip" /f >nul
echo [OK] All "Copy Path" entries installed.
pause
goto menu

:remove_file
REG DELETE "HKCR\*\shell\CopyFilePath" /f >nul 2>&1
echo [OK] File entry removed.
pause
goto menu

:remove_folder
REG DELETE "HKCR\Directory\shell\CopyFolderPath" /f >nul 2>&1
echo [OK] Folder entry removed.
pause
goto menu

:remove_background
REG DELETE "HKCR\Directory\Background\shell\CopyCurrentPath" /f >nul 2>&1
echo [OK] Background entry removed.
pause
goto menu

:remove_all
REG DELETE "HKCR\*\shell\CopyFilePath" /f >nul 2>&1
REG DELETE "HKCR\Directory\shell\CopyFolderPath" /f >nul 2>&1
REG DELETE "HKCR\Directory\Background\shell\CopyCurrentPath" /f >nul 2>&1
echo [OK] All entries removed.
pause
goto menu

Advanced: Customizing the Menu Position and Visibility

Moving to the Top

REG ADD "HKCR\*\shell\CopyFilePath" /v "Position" /d "Top" /f

Show Only on Shift+Right-Click

REG ADD "HKCR\*\shell\CopyFilePath" /v "Extended" /t REG_SZ /d "" /f

Custom Icon

You can use any .ico file or system DLL icon:

REG ADD "HKCR\*\shell\CopyFilePath" /v "Icon" /d "C:\path\to\icon.ico" /f

Best Practices

  1. Always include an uninstaller alongside the installer script.
  2. Test with paths that contain spaces to ensure the clipboard content is correct.
  3. Use shell32.dll,134 for a clipboard-style icon, or choose a custom icon for branding.
  4. Run as administrator since HKCR modifications require elevated privileges.
  5. Consider quoting behavior and choose whether you want the clipboard to include or exclude quotation marks around the path.

Conclusion

Adding a "Copy Path" option to the Windows context menu is a small but highly impactful productivity boost. This Batch Script approach gives you full control over where the option appears (files, folders, or background), what icon is displayed, and how the path is formatted. Once installed, it becomes one of those tools you use so frequently that you forget it is not a default Windows feature.