[windows] "Register" an .exe so you can run it from any command line in Windows

How can you make a .exe file accessible from any location in the Windows command window? Is there some registry entry that has to be entered?

This question is related to windows

The answer is


You need to make sure that the exe is in a folder that's on the PATH environment variable.

You can do this by either installing it into a folder that's already on the PATH or by adding your folder to the PATH.

You can have your installer do this - but you will need to restart the machine to make sure it gets picked up.


Add to the PATH, steps below (Windows 10):

  1. Type in search bar "environment..." and choose Edit the system environment variables which opens up the System Properties window
  2. Click the Environment Variables... button
  3. In the Environment Variables tab, double click the Path variable in the System variables section
  4. Add the path to the folder containing the .exe to the Path by double clicking on the empty line and paste the path.
  5. Click ok and exit. Open a new cmd prompt and hit the command from any folder and it should work.

it's amazing there's no simple solution for such a simple task on windows, I created this little cmd script that you can use to define aliases on windows (instructions are at the file header itself):

https://gist.github.com/benjamine/5992592

this is pretty much the same approach used by tools like NPM or ruby gems to register global commands.


Should anyone be looking for this after me here's a really easy way to add your Path.

Send the path to a file like the image shows, copy and paste it from the file and add the specific path on the end with a preceding semicolon to the new path. It may be needed to be adapted prior to windows 7, but at least it is an easy starting point.

Command Prompt Image to Export PATH to text file


  • If you want to be able to run it inside cmd.exe or batch files you need to add the directory the .exe is in to the %path% variable (System or User)
  • If you want to be able to run it in the Run dialog (Win+R) or any application that calls ShellExecute, adding your exe to the app paths key is enough (This is less error prone during install/uninstall and also does not clutter up the path variable)

This worked for me:

  • put a .bat file with the commands you need (I use to run .py script into this) into a FOLDER,
  • go in the variable environment setting (type var in the search bar and it will show up)
  • in the global settings
    • choose path,
    • then modify,
    • then add the path to your .bat file (without the .bat file)
    • close everything: done.

Open the cmd, write the name of the .bat file and it will work


Windows 10, 8.1, 8

Open start menu,

  1. Type Edit environment variables
  2. Open the option Edit the system environment variables
  3. Click Environment variables... button
  4. There you see two boxes, in System Variables box find path variable
  5. Click Edit
  6. a window pops up, click New
  7. Type the Directory path of your .exe or batch file ( Directory means exclude the file name from path)
  8. Click Ok on all open windows and restart your system restart the command prompt.

It is very simple and it won't take more than 30 seconds.

For example the software called abc located in D:/Softwares/vlc/abc.exe Add the folder path of abc.exe to system environment variables.

My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables

enter image description here

enter image description here

Click on Ok.

now you can just open cmd prompt and you can launch the software from anywhere. to use abc.exe just type abc in the command line.


Let's say my exe is C:\Program Files\AzCopy\azcopy.exe

Command/CMD/Batch

SET "PATH=C:\Program Files\AzCopy;%PATH%"

PowerShell

$env:path = $env:path + ";C:\Program Files\AzCopy"

I can now simply type and use azcopy from any location from any shell inc command prompt, powershell, git bash etc


Use a 1 line batch file in your install:

SETX PATH "C:\Windows"

run the bat file

Now place your .exe in c:\windows, and you're done.

you may type the 'exename' in command-line and it'll run it.


In order to make it work

You need to modify the value of the environment variable with the name key Path, you can add as many paths as you want separating them with ;. The paths you give to it can't include the name of the executable file.

If you add a path to the variable Path all the excecutable files inside it can be called from cmd or porweshell by writing their name without .exe and these names are not case sensitive.


Here is how to create a system environment variable from a python script:

It is important to run it with administrator privileges in order to make it work. To better understand the code, just read the comments on it.

Tested on Windows 10

import winreg

# Create environment variable for call the program from shell, only works with compiled version
def environment_var(AppPath):

    # Point to the registry key of the system environment variables
    key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, r'System\CurrentControlSet\Control\Session Manager\Environment')

    def add_var(path):
        # Add the variable
        winreg.SetValueEx(key, 'Path', 0, winreg.REG_SZ, path)
        winreg.CloseKey(key)

    try:
        # Try to get the value of the Path variable
        allPaths = winreg.QueryValueEx(key, 'Path')[0]
    except Exception:
        # Create the Path variable if it doesn't exist
        add_var(path=AppPath)
        return        

    # Get all the values of the existing paths
    Path=allPaths.split(';')

    # If the Path is empty, add the application path
    if Path == ['']:
        add_var(path=AppPath)
        return

    # Check if the application path is in the Path variable
    if AppPath not in Path:
        # Add the application path to the Path environment variable and add keep the others existing paths
        add_var(path=AppPath+';'+allPaths)

# Only run this if the module is not imported by another
if __name__ == "__main__":
    # Run the function
    environment_var(AppPath=".")

You can find more information in the winreg documentation


You may also permanently (after reboots) add to the Path variable this way:

Right click My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables

Reference: Change System/User Variables


Rather than putting the executable into a directory on the path, you should create a batch file in a directory on the path that launches the program. This way you don't separate the executable from its supporting files, and you don't add other stuff in the same directory to the path unintentionally.

Such batch file can look like this:

@echo off
start "" "C:\Program Files (x86)\Software\software.exe" %*

You have to put your .exe file's path into enviroment variable path. Go to "My computer -> properties -> advanced -> environment variables -> Path" and edit path by adding .exe's directory into path.

Another solution I personally prefer is using RapidEE for a smoother variable editing.


Put it in the c:\windows directory or add your directory to the "path" in the environment-settings (windows-break - tab advanced)

regards, //t


Another way could be through adding .LNK to your $PATHEX. Then just create a shortcut to your executable (ie: yourshortcut.lnk) and put it into any of the directories listed within $PATH.

WARNING NOTE: Know that any .lnk files located in any directories listed in your $PATH are now "PATH'ed" as well. For this reason, I would favor the batch file method mentionned earlier to this method.


The best way to do this is just install the .EXE file into the windows/system32 folder. that way you can run it from any location. This is the same place where .exe's like ping can be found


You can add the following registry key:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\myexe.exe

In this key, add the default string value containing the path to the exe file.


Simple Bash-like aliases in Windows

To get global bash-like aliases in Windows for applications not added to the path automatically without manually adding each one to the path, here's the cleanest solution I've come up with that does the least amount of changes to the system and has the most flexibility for later customization:

"Install" Your Aliases Path

mkdir c:\aliases
setx PATH "c:\aliases;%PATH%"

Add Your Alias

Open in New Shell Window

To start C:\path to\my program.exe, passing in all arguments, opening it in a new window, create c:\aliases\my program.bat file with the following contents(see NT Start Command for details on the start commmand):

@echo off
start "myprogram" /D "C:\path to\" /W "myprogram.exe" %*

Execute in Current Shell Window

To start C:\path to\my program.exe, passing in all arguments, but running it in the same window (more like how bash operates) create c:\aliases\my program.bat file with the following contents:

@echo off
pushd "C:\path to\"
"my program.exe" %*
popd

Execute in Current Shell Window 2

If you don't need the application to change the current working directory at all in order to operate, you can just add a symlink to the executable inside your aliases folder:

cd c:\aliases\
mklink "my program.exe" "c:\path to\my program.exe"