45

I have a feeling I should be able add a directory to the PATH environment variable on an application-lifetime basis, but I can't find out how to do this. Is it possible to add a parameter to a Windows shortcut that appends a directory to the current value of PATH for use by the application being linked?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user366203
  • 453
  • 1
  • 4
  • 4

4 Answers4

41

As explained here: http://www.labs64.com/blog/2012/06/set-environment-variables-in-windows-shortcut/ you can do it without a bat file too.

Set Target to e.g.:

C:\Windows\System32\cmd.exe /c "SET path=%path%&& START /D ^"C:\Program Files (x86)\Notepad++^" notepad++.exe"

To avoid see the command prompt for a split second before it close again, you should set

Run: Minimized 

on the Shortcut tab

(Tested on Windows 7, Windows 10)

Jens
  • 2,327
  • 25
  • 34
  • 1
    Seems that after setting path no space required before &&, because new path will have extra space (tested on Win 10). I.e. it should be: C:\Windows\System32\cmd.exe /c "SET path=%path%&& START /D ^"C:\Program Files (x86)\Notepad++^" notepad++.exe" – user2399321 Oct 11 '18 at 08:02
  • @user2399321 I was not able to reproduce the extra space with the following command: `C:\Windows\System32\cmd.exe /c "SET path=%path% && echo '%path%'"` it outputs `'C:\Path1;...;C:\PathN'` (Tested on Windows 10) – Jens Oct 16 '18 at 08:14
  • Sorry, but your test is slightly wrong, i.e. %path% is not actually updated, you can check this actually changing path variable `C:\Windows\System32\cmd.exe /c "SET path=%path%;appended_text && echo '%path%'"` It will output path without "appended_text". The proper test case is probably to use separate bat file: echo_path.bat `echo [%path%]` and update the command line: `cmd.exe /c "SET path=%path%;appended_text && START /D ^".^" echo_path.bat"` – user2399321 Oct 18 '18 at 07:21
  • Corrected test confirmed @user2399321's finding `C:\Windows\System32\cmd.exe /v:on /c "SET path=%path%;x && echo '!path!'"` – Jens Oct 18 '18 at 08:01
  • 1
    Although your mini program works I prefer to use this version because I use an app with a space in the name, and it simplifies things ```C:\Windows\System32\cmd.exe /c "SET path=%path%&& START ^"^" ^"C:\Program Files (x86)\Notepad++\notepad++.exe^"" ```. Your version of this code does not allow me to run an exe called `Fun time.exe` – Samuel Thompson Nov 08 '18 at 22:12
  • thanks @SamuelThompson I first struggled with your example because of the word wrapping after `^"^"` and omitted the space before `^"C:\...` Had a similar situation and had to pass a parameter as well `C:\Windows\System32\cmd.exe /c "SET NODE_TLS_REJECT_UNAUTHORIZED=0&& START ^"^" ^"C:\Program Files (x86)\Bot Framework Emulator\Bot Framework Emulator.exe^" --remote-debugging-port=8881"` – martinoss Jan 22 '19 at 12:40
34

Let the shortcut execute a batch file (.cmd), that

  • Sets the environment variable
  • execute the app
  • You use "START" to execute the app, this will start the app in another process, but it will copy the environment. You do not wait for the app to finish.
  • Now you can exit the batch file.

Should look like this:

@echo off
set path=%path%;C:\My Folder
start "Window Title" "Path to my exe"
Pharap
  • 3,826
  • 5
  • 37
  • 51
GvS
  • 52,015
  • 16
  • 101
  • 139
  • 8
    Any workaround for windows 7 taskbar? I have a shortcut there with such environment variables modification - another icon appears with proper process running. – Wojciech Jul 06 '12 at 06:39
  • 12
    You might need to change it to `start "" "Path to my exe"` as the `start` command could interpret the first quoted string as the window title... – aschipfl Aug 22 '16 at 17:45
  • What @aschipfl said absolutely needs to be done, it didn't work for me otherwise. Can I edit? – Stefan Monov Aug 23 '16 at 10:43
  • 2
    It looks like this approach stopped working after update to Windows 10 Version 1809 – AlexK Dec 28 '18 at 03:28
  • There's a `.ini` file same folder as my `.exe` that overrode these environment variables. So check for a `.ini` file if variables aren't being set as expected. – Philip Rego May 16 '22 at 16:36
4

Linking directly to a batch file spawns an annoying console that you probably want to avoid. Here's a work-around. The simpler solution is to use the "Start Minimized" option in your link, but on Windows 7 you'll see a momentary console light up your task bar.

start.bat:

@echo off
IF "%1" == "" GOTO Error
IF "%2" == "" GOTO Error
IF NOT EXIST %2 GOTO Error
SET PATH=%1;%PATH%
start %2
GOTO End

:Error
echo Problem!
pause

:End

shortcut target:

MyPath = "C:\MyApp"
Set shell = WScript.CreateObject("WScript.Shell")
cmd = "start.bat " & MyPath & " MyApp.exe"
shell.Run cmd, 0, false
Set env = Nothing
Set shell = Nothing
Will Bickford
  • 5,381
  • 2
  • 30
  • 45
2

You can do this with PowerShell easily. PowerShell exposes environment variables using the $env: prefix. For example, I wanted to launch TeamSQL with custom JAVA_HOME and PATH environment variables, so I could connect to a PostgreSQL database. TeamSQL depends on JDK / OpenJDK for this purpose.

First, I downloaded pre-built OpenJDK and extracted the ZIP archive with 7-Zip.

Next, in PowerShell, I ran the following:

$env:JAVA_HOME='C:\Users\TrevorSullivan\Downloads\openjdk\jdk-11.0.2\'
$env:PATH += ';%JAVA_HOME%\bin'

# Launch TeamSQL
& C:\Users\TrevorSullivan\AppData\Local\Programs\TeamSQL\TeamSQL.exe

Store that PowerShell code in a .ps1 file, and you can run it with PowerShell. Because child processes inherit the environment variables from the PowerShell session, your program is good to go.