Best practice for Windows PATH/environment variables managing?

22

10

Right now, it's such a mess. Every time I install a new program that does something trivial in the command line, I have to add it to the path. In fact, I'm developing on Ruby and even gems (plugins/extensions) sometimes have their own executables, and are stored in different folders and I end up having to add several paths per application, as well. It's gotten to the point that I am pushing thirty paths in my PATH environment variable.

I'm cautious to copying and pasting them to a home folder of sorts and setting path to that; it just feels wrong. Is there an established best practice that I am missing?

Justin L.

Posted 2010-06-23T03:03:17.930

Reputation: 959

This is such a great question - running programs is so easy on UNIX but such a pain on Windows. Its a shame that the only solutions still seem like such hard work... – user10550 – 2017-02-26T17:30:40.743

Answers

13

A number of small self-contained utilities reside in my %UserProfile%\Apps folder, such as archivers and the like. Then there are a number of batch files I wrote and frequently use which reside in %UserProfile%\Batches. For the rest, I just add them to the PATH, which is either done by an installer or with

setx PATH "%PATH%;%CD%"

from the appropriate directory. The number of paths there should probably only ever become an issue if the contents get too long (there are length limits for environment variables).

You can (and I sometimes do) write small wrapper batch files for such programs. Simply copying/hardlinking/symlinking the executable into another directory will likely not work on Windows but you can easily create a batch file, for example for the program Foo:

@"%ProgramFiles%\Foosoft Foo\foo.exe" %*

and you can then add the folder where you store those batch files to your PATH.

Joey

Posted 2010-06-23T03:03:17.930

Reputation: 36 381

How do these wrapper batch files work? Do they work exactly as requiring the actual path? If so, this is amazing. – Justin L. – 2010-06-23T08:36:10.537

@Justin: They just delegate all arguments given to the batch file to the actual program. Where that resides you'll have to know yourself. – Joey – 2010-06-23T08:57:34.023

Hm; I should clarify -- is there any functional difference between adding the binary's directory to PATH and creating a wapper batch file of the binary into a custom directory that is on the PATH? – Justin L. – 2010-06-23T09:18:18.507

@Justin: If you use the batch files in batch files again, then there is. Aside from that I'm not aware of any. – Joey – 2010-06-23T10:03:28.520

@Joey I like the wrapper idea, good for launching programs from cmd, but when launching from start..run it's a distraction for a cmd window to jump up before the program launches, do you have a way around that? it seems one may as well put the directories for all the programs in the path and let it be long. so near yet so far! – barlop – 2012-03-19T01:50:26.080

@barlop: I tend to only do that for console programs anyway ... in my case C, C#, VB compilers, Python, Ruby, &c. and for all those I most certainly expect them to use a console ;-). So no, I don't have a solution. – Joey – 2012-03-19T08:26:28.197

10

I am not sure about any specific best practices. But I prefer using GUI PATH Editor tools to manage PATH variables. So that it can be clearly maintained.

List of PATH Editor -

Rapid Environment Editor

Redmond PATH Editor

ukanth

Posted 2010-06-23T03:03:17.930

Reputation: 9 930

Graphical editors might make things a lot easier heh – Justin L. – 2010-06-23T18:36:26.570

That Redmond PATH editor is exactly the sort of thing I was looking for. +1 – Nathan Ridley – 2010-08-19T11:27:24.027

The question was about MANAGING the variable, not only adding the items . So this is the most correct answer and IMO the best practice as other solutions hide most of the content of the PATH variable. – Maciej Beimcik – 2017-09-27T06:07:41.833

2

Now that most Windows machines come with PowerShell, I use many sal/Set-Alias commands in my user profile, so if an app has only one or two apps that I use I'll create aliases for just those two commands instead of adding the whole app folder to the path. Examples include SQL Management Studio, Notepad++, TFS Power Tools (command-line tool, tfpt.exe). I also copy my user profile across machines, so this lets me check for the existence of that app on the current machine before creating the alias (sometimes warning if app isn't installed).

filter ctQuoteString { "`"$_`"" }
filter ctResolvePath { Resolve-Path $_ | select -ExpandProperty Path | ctQuoteString } # used in Edit.ps1

$nppExe = "C:\Program Files (x86)\Notepad++\notepad++.exe"
if ((Test-Path variable:\nppExe) -and (Test-Path $nppExe)) {
    function EditNotepadPP { 
        param ([parameter(ValueFromPipelineByPropertyName=$true)][Alias("FullName","FileName")]$Path) 
        begin { if (! $nppExe) { throw 'variable $nppExe is not defined' } }
        process {
            $Path | ctResolvePath | % { # ctResolvePath will get full path and surround with quotes
                & $nppExe $_
            }
            #AddEditHistory $Path #if you need detailed time tracking, might help to create a log what files you're editting
       } 
    }
    Set-Alias npp EditNotepadPP
}

# I have similar functions for other apps.
Set-Alias vs EditVS
Set-Alias tfe EditTFCheckout

yzorg

Posted 2010-06-23T03:03:17.930

Reputation: 711