Add an Open Anaconda Prompt context menu item to folders in Windows

Ever wanted to open an Anaconda Prompt by right clicking the folder in Explorer in Windows 10? Git Bash has an entry in the right-click context menu and so does Powershell (just Shift + Right-Click). Anaconda doesn't have one by default and I wanted to add it. But none of the answers on Stack Overflow or a bunch of other sites worked. I finally got it working after reading the Wikipedia entry for cmd.exe.

Batch file

Here is the batch file:

REM Run this as administator. 
REM Check the path to the activate script depending where you installed Anaconda / version.

REG ADD HKCR\Directory\Background\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\Background\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "cmd.exe /k pushd %%v && %%USERPROFILE%%\Anaconda3\Scripts\activate.bat"
REG ADD HKCR\Directory\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "cmd.exe /k pushd %%v && %%USERPROFILE%%\Anaconda3\Scripts\activate.bat"

Save it as “anaconda.bat” and run it as Administrator and it will install the correct registry keys. After running this script, the prompt will be in your right-click context menu for each folder in Explorer.

I'll attempt to explain how this works:

  • There are separate entries for Directory\Background\shell and Directory\shell. I believe the Background entry is for right-clicking on the background / empty part of the folder in Explorer (right pane) and the plain Shell entry is for right-clicking the folder itself (in either the left or right panes).
  • Both entries call the same command
  • cmd.exe /k [command] runs the command specified. In this case, it is pushd %%v && %%USERPROFILE%%\Anaconda3\Scripts\activate.bat which is two commands put together.
    • The pushd %%v switches the prompt to the folder being clicked. There are two % because it is an escaped character in the batch file.
    • && strings two commands to be executed in sequence
    • %%USERPROFILE%%\Anaconda3\Scripts\activate.bat is the command to activate the Anaconda environment, if you installed it in your user folder. It will be stored in the registry as %USERPROFILE%\Anaconda3\Scripts\activate.bat, which means each user will use their own copy of activate.bat in their own folder.

Discussion

Mark, 2021/01/27 08:03
Peter,

many thanks for your post. Check out the following script which word for me. It even adds the menu for library folders. Also check %%1 instead of %%v to start with the correct folder!

Greetings, Mark from Germany

REG ADD HKCR\Directory\Background\shell\Anaconda\ /ve /f /d "Open in Anaconda"
REG ADD HKCR\Directory\Background\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\miniconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\Background\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%windir%\System32\cmd.exe /K pushd %%v && %%USERPROFILE%%\miniconda3\Scripts\activate.bat"

REG ADD HKCR\Directory\shell\Anaconda\ /ve /f /d "Open in Anaconda"
REG ADD HKCR\Directory\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\miniconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%windir%\System32\cmd.exe /K pushd %%1 && %%USERPROFILE%%\miniconda3\Scripts\activate.bat"

REG ADD HKCR\LibraryFolder\background\shell\Anaconda\ /ve /f /d "Open in Anaconda"
REG ADD HKCR\LibraryFolder\background\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\miniconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\LibraryFolder\background\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%windir%\System32\cmd.exe /K pushd %%v && %%USERPROFILE%%\miniconda3\Scripts\activate.bat"
Peter Yu, 2021/02/05 16:36
I find it very interesting that no one has the same exact same answer to this (e.g. the %%1 vs %%v, where I presume %%v didn't work for you but it did work for me).
Saurav Prashar, 2021/02/24 01:31
Hi I am new to coding and wanted to have this feature. I have not installed anaconda in the default directory can anyone tell what should I change to get it working. It is saying that path not found.
Saurav Prashar, 2021/02/24 01:43
I just figured it out, Now its working, Thanks
I would love to hear your feedback. Enter your comment below [ Terms of Use ]:
CQSVX
 

About Peter Yu I am a research and development professional with expertise in the areas of image processing, remote sensing and computer vision. I received BASc and MASc degrees in Systems Design Engineering at the University of Waterloo. My working experience covers industries ranging from district energy to medical imaging to cinematic visual effects. I like to dabble in 3D artwork, I enjoy cycling recreationally and I am interested in sustainable technology. More about me...

Feel free to contact me with any questions about this site at [user]@[host] where [user]=web and [host]=peteryu.ca

Copyright © 1997 - 2021 Peter Yu