none
Best practice for adding application to $env:Path in PowerShell? RRS feed

  • Question

  • I'm trying to figure out the best way to add a program to PS's path. When I look at $env:Path, I see tons of different entries pointing to various programs on the system that seem to have been added by their installers. This would suggest that I could just add another entry pointing to the .exe I want to have available in the path, but if I do this every time I want to have an application available at the shell my path is going to get hideously cluttered.

    In bash, I would normally make a bin directory within my home directory, add it to the path at startup in my .bashrc, and then make symlinks in that directory for anything I wanted available. This keeps the path clean and easy to manage. Is there a way to do something like this in PS, or is the standard solution just to add a new entry to the path for every application you want until it's like a million miles long and unreadable? Putting symlinks or shortcuts in directories already in the path hasn't worked so far.


    • Edited by zaaanderson Wednesday, April 9, 2014 8:24 PM
    Wednesday, April 9, 2014 8:22 PM

Answers

  • It depends on your needs/goals:

    • If you want the directory to be in the path for every user of the machine, add the directory to the per-machine path (this requires administrative access).
    • If you want the directory to be available to the current user only, add it to the current user path.
    • If you want the directory to be available only to the current process (and processes it starts), then add the directory the PATH environment variable for the current process.

    I wrote a small command-line tool call editpath.exe that lets you easily add, remove, or list the directories in your path:

    http://www.westmesatech.com/misctools.html (epath2.zip)


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 9, 2014 8:39 PM
    Moderator

All replies

  • It depends on your needs/goals:

    • If you want the directory to be in the path for every user of the machine, add the directory to the per-machine path (this requires administrative access).
    • If you want the directory to be available to the current user only, add it to the current user path.
    • If you want the directory to be available only to the current process (and processes it starts), then add the directory the PATH environment variable for the current process.

    I wrote a small command-line tool call editpath.exe that lets you easily add, remove, or list the directories in your path:

    http://www.westmesatech.com/misctools.html (epath2.zip)


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 9, 2014 8:39 PM
    Moderator
  • I'm trying to figure out the best way to add a program to PS's path. When I look at $env:Path, I see tons of different entries pointing to various programs on the system that seem to have been added by their installers. This would suggest that I could just add another entry pointing to the .exe I want to have available in the path, but if I do this every time I want to have an application available at the shell my path is going to get hideously cluttered.

    In bash, I would normally make a bin directory within my home directory, add it to the path at startup in my .bashrc, and then make symlinks in that directory for anything I wanted available. This keeps the path clean and easy to manage. Is there a way to do something like this in PS, or is the standard solution just to add a new entry to the path for every application you want until it's like a million miles long and unreadable? Putting symlinks or shortcuts in directories already in the path hasn't worked so far.


    You can't put symlinks in a folder to a folder but you must put a link or shortcut to the executable.  Links to folders are not scanned. Only the root folder in the path is scanned.  No programs wil lever be found through the path if it is in the current folder in a PowerShell session.

    You can quickly alter the patch for the current session like this:

    $env:path+='c:\myprogramfolder'

    When you exit PowerShell it will not reamian.  It will only change the PowerShell process path for that PowerShell session.


    ¯\_(ツ)_/¯

    Wednesday, April 9, 2014 9:01 PM
  • Do you still need help with this question?

    -- Bill Stewart [Bill_Stewart]

    Monday, June 9, 2014 7:59 PM
    Moderator