I am currently doing development on a Windows 10 machine for a client, and I found myself repeating a bunch of tasks related to the build and deployment process quite frequently. I had already written a few PowerShell scripts to automate much of the boring stuff, but I thought it would be neat to be able to run the scripts by just right-clicking on a file or folder and clicking an item in the context menu. I discovered that this is possible by simply adding a few keys to the Windows registry.
In the sections below we will go through how to add context menu items for folders, files with a specific extension and how to export the created keys to a registration (.reg
) file. Everything will be done in the Registry Editor which you can find by searching in the start menu or by opening the “Run” prompt (Ctrl+R) and typing “regedit”.
Adding menu items for folders
To add a context menu item when right-clicking on a folder, add a new key under
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell
or HKEY_CURRENT_USER\SOFTWARE\Classes\directory\shell
depending on whether you want the menu item to be available for all users or just for yourself. The name of the key is what will be shown in the right-click menu. Then create another key under the one you just created and name it “command”. The value of this key is the command you want to run when the menu item is pressed. For example, if I wanted a menu item called “Say Hello” that says hello and echoes the path of the folder, I would add the key:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\Say Hello\command
and set the default value of the “command” key to:
cmd /k echo Hello %1
The %1
is a placeholder for the path of the folder that was right-clicked. If we open up Windows Explorer and right-click on C:\Windows
we will now see an item named “Say Hello”. If we click it, a command prompt pops up with the message:
Hello C:\Windows
C:\>
To run a PowerShell script with the folder path as input we could set the command value to:
powershell -ExecutionPolicy Bypass -NoExit MyScript.ps1 %1
If you want the terminal to close automatically after the script exits, just omit the /k
or -NoExit
flag.
Adding menu items for files with a specific extension
Let us say we have a custom file type with the extension .abc
. To add a context menu item for this extension specifically, we would basically do the same as we did for folders, but add the key under:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.abc\shell
After adding the key, you should see the new menu item only when you right-click a file with the specified extension. I found a few posts on Google saying that it should be possible to add it for just the current user by creating the key under
HKEY_CURRENT_USER\SOFTWARE\Classes\.abc\shell
but for some reason I can’t get this to work on my end. Since I am the only person using my machine, I haven’t bothered to look further into this, but I would be very interested if anyone finds a solution.
Export registration files
In the Registry Editor you can export your keys to a .reg
file, which makes it easy to add the same keys on another machine. If we select the Say Hello/command
key we created earlier, click File > Export, save the file and open it with a text editor, we see the following content:
Windows Registry Editor Version 5.0
[HKEY_CURRENT_USER\SOFTWARE\Classes\directory\shell\Say Hello\command]
@="cmd /k echo Hello %1"
We can add multiple keys to the same file and then just double-click the file to merge all the keys into the registry.