Search Results

Windows shell extension to easily add any folder to the PATH

How many times has this happened to you? You’ve downloaded a new Windows command-line tool and want to be able to use it from wherever your current directory happens to be, but the only way to add a folder to Windows’ system PATH is via an unwieldy and hard-to-find modal dialog in the System Properties’ [...]

How many times has this happened to you? You’ve downloaded a new Windows command-line tool and want to be able to use it from wherever your current directory happens to be, but the only way to add a folder to Windows’ system PATH is via an unwieldy and hard-to-find modal dialog in the System Properties’ Advanced tab. Well, I had just this problem last week when I was configuring some new machines and wanted to add some of my favorite command-line tools (mostly ports of Linux/Unix tools like which and less). I was about to go through System Properties to add their directory but figured there had to be a better way.

After a few minutes of Googling, I found this post by “/\/\o\/\/” which demonstrated a way of using Windows PowerShell (the next generation advanced command-line environment for Windows) to add a shell extension, used by right-clicking on any Explorer folder, which adds the selected folder to the system PATH.

Unfortunately, the code shown in the post didn’t quite work as-is and besides I’m not too comfortable yet in PowerShell. So I decided to fix the code and build a script that does the same thing but from CMD (or by double-clicking). It’s very simple to use; just execute the script from a CMD session or double-click it in Explorer. To uninstall, just run it again; it automatically backs out its changes when run a second time.

License and acknowledgement: The original (though non-working) code sample is by “/\/\o\/\/” and linked above; I claim no credit for it. However, I made it work and packaged it into a user-friendly script and so claim copyright over that portion under the GPL.

With that out of the way, here’s the code:


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: AddToPath.cmd ::
:: Written by Andrew Rich (andrew.rich@project-insomnia.com) ::
:: Copyright (c) Andrew Rich under GPL (http://gnu.org/licenses/gpl.html) ::
:: Based on code by "/\/\o\/\/" at http://bit.ly/58hYYh ::
:: ::
:: This command script adds an Explorer shell extension which, when ::
:: selected on right-clicking a folder, will add it to the system PATH. The ::
:: change takes effect immediately for new CMD or PowerShell instances ::
:: started after adding the folder to the PATH. CMD or PowerShell instances ::
:: started adding the folder will continue to use the PATH in effect at the ::
:: time they were started. ::
:: ::
:: Usage: ::
:: Double-click AddToPath.cmd or execute AddToPath.cmd from a CMD session. ::
:: The only command-line parameter available is /? which shows this text. ::
:: ::
:: Requirements: ::
:: - Windows XP SP3 or later. Not tested on Vista or Windows 7 BUT should ::
:: work unaltered. Please send feedback if you run AddToPath on Vista or ::
:: Windows 7. ::
:: - The user executing the command script must be a local Administrator. ::
:: However, the PATH update will affect all users. ::
:: - reg.exe must be on the system PATH. This should be the case for any ::
:: properly functioning Windows machine. ::
:: - Microsoft PowerShell must be properly installed. This should be the ::
:: case for any Windows machine which is current on Windows updates. The ::
:: command script will verify that PowerShell is installed. If the script ::
:: reports that PowerShell is not installed, download and install it from ::
:: http://bit.ly/mYzg4. ::
:: ::
:: Uninstall/remove: ::
:: Just run AddToPath.cmd again. If the "Add To Path" shell extension ::
:: installed by AddToPath.cmd exists, running the command script a second ::
:: time will cause the shell extension to be removed. ::
:: ::
:: Manual uninstall: ::
:: From a CMD session, type: ::
:: ::
:: reg delete HKCR\Folder\Shell\Add_To_Path ::
:: ::
:: Note that uninstalling only removes the Explorer shell extension, and ::
:: does not affect any system PATH entries which may have been added. ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::ENDHEADERCOMMENT

@Echo Off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: setup
SET ShellExtKey=HKCR\Folder\Shell\Add_To_Path
SET ShellExtKeyText="Add to Path"
SET PSPathKey=HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

:: check command params
IF [%1]==[] GOTO CmdParamsDone
IF /I %1 EQU --help GOTO ShowHelp
IF /I %1 EQU /? GOTO ShowHelp
IF /I %1 EQU -? GOTO ShowHelp
GOTO ErrBadParam

:ShowHelp
ECHO.
FOR /F "delims=" %%A IN (%~fs0) DO (
IF /I %%A EQU ::ENDHEADERCOMMENT GOTO :EOF
ECHO %%A
)
PAUSE
GOTO :EOF

:ErrBadParam
ECHO.
ECHO "%1" is not a recognized command-line parameter for %0.
ECHO Type %0 /? for help.
ECHO.
PAUSE
GOTO :EOF

:CmdParamsDone
:: check overall prerequisite: reg.exe must be on the system PATH
reg 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%]==[0] GOTO ErrNoRegExe

:: is the Add_To_Path shell extension already installed?
:: if the reg query fails (errorlevel=1) then it's not installed
reg query %ShellExtKey% 2>>NUL 1>>&2
IF [%ERRORLEVEL%]==[1] GOTO Install
GOTO UnInstall

:Install
:: check install prerequisite: PowerShell must be installed
:: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell should contain the full path to powershell.exe
:: e.g. C:\WINNT\system32\WindowsPowerShell\v1.0\powershell.exe
FOR /F "usebackq tokens=3" %%a IN (`reg query %PSPathKey% /v Path ^| FIND "powershell.exe"`) DO SET PSPATH=%%a
IF NOT DEFINED PSPATH GOTO :ErrNoPowerShell
IF [%PSPATH%] EQU [] GOTO :ErrNoPowerShell

:: build the shell extension's command line
:: does powershell.exe's full path include spaces? if so, add quotes
FOR /F "tokens=2" %%a IN ("%PSPATH%") DO SET s=%%a
IF DEFINED s (
SET AddToPathCmd="%PSPATH%"
) ELSE (
SET AddToPathCmd=%PSPATH%
)
SET AddToPathCmd=%AddToPathCmd% -NonInteractive -NoProfile -Command [system.environment]::setEnvironmentVariable('path',$env:path + ';%%1','machine')

:: create the shell extension's base key under Folder\Shell />reg add %ShellExtKey% /ve /d %ShellExtKeyText% /f 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%] EQU [0] GOTO ErrRegAdd1

:: add the command line
reg add %ShellExtKey%\Command /ve /d "%AddToPathCmd%" /f 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%] EQU [0] GOTO ErrRegAdd2

ECHO Installed. Right-click on a folder in Explorer to add it to the system PATH.
PAUSE
GOTO :EOF

:UnInstall
reg delete %ShellExtKey% /f 2>>NUL 1>>&2
IF NOT [%ERRORLEVEL%] EQU [0] GOTO ErrRegDel
ECHO UnInstalled.
PAUSE
GOTO :EOF

:ErrNoRegExe
ECHO Error: reg.exe was not found on the system PATH.
PAUSE
GOTO :EOF

:ErrNoPowerShell
ECHO Error: Windows PowerShell is not installed (or is not installed correctly).
ECHO Go to http://bit.ly/mYzg4 to download and install Windows PowerShell.
PAUSE
GOTO :EOF

:ErrRegAdd1
ECHO Error creating the shell extension's base key.
ECHO Run %0 again to back out any changes.
PAUSE
GOTO :EOF

:ErrRegAdd2
ECHO Error adding the shell extension's command line.
ECHO Run %0 again to back out any changes.
PAUSE
GOTO :EOF

:ErrRegDel
ECHO Error removing the shell extension's registry entry.
ECHO To remove manually, type the following at the CMD prompt:
ECHO reg delete HKCR\Folder\Shell\Add_To_Path
PAUSE
GOTO :EOF

You can copy, paste and save that as AddToPath.cmd, or just download it (I recommend downloading it because line wrapping doesn’t show properly here). AddToPath.cmd.zip

I hope this is useful. Please send me feedback if you use it!

Leave a Comment

Palo Alto, CA to New York City, NY

Ask me to drive cross-country and this is what you get. This is not actually the route I'd pick, though it is what you get if you ask Google for "Palo Alto to New York" without any specifications. I'd probably stick with the suggested route early on, through Tahoe and Reno, and then branch off [...]

Ask me to drive cross-country and this is what you get.

This is not actually the route I'd pick, though it is what you get if you ask Google for "Palo Alto to New York" without any specifications. I'd probably stick with the suggested route early on, through Tahoe and Reno, and then branch off the Interstate a bit.

Leave a Comment

Lesson Learned

So in Ant, properties set when calling a subtarget from the “depends” clause of a target are accessible to the parent target, but properties set when calling a target with “antcall” are not. Gotcha. That means I had to change this: to this: The upshot of which is that my “depends” line gets unmanageably long [...]

So in Ant, properties set when calling a subtarget from the “depends” clause of a target are accessible to the parent target, but properties set when calling a target with “antcall” are not. Gotcha. That means I had to change this:










to this:








The upshot of which is that my “depends” line gets unmanageably long as I add supported versions, and I call each and every one of the setPathsFor targets. Is there a better way to do this?

Leave a Comment

Unexpected but I’m not complaining

The “Service Engine Soon” light came on as I was driving home Tuesday night in my ’99 Pontiac Grand Prix GTP, so I made an appointment for this morning at my local GM dealer to have it checked out. The light stayed on through my commute to and from work yesterday, but was mysteriously dark [...]

The “Service Engine Soon” light came on as I was driving home Tuesday night in my ’99 Pontiac Grand Prix GTPPontiac Grand Prix GTP, so I made an appointment for this morning at my local GM dealer to have it checked out. The light stayed on through my commute to and from work yesterday, but was mysteriously dark this morning (and was lit during the normal systems check upon starting the car, so I know the lightbulb is not out). Not knowing what to think, I kept the appointment and dropped the car off this morning. Apparently this can be caused by any number of problems from trivial to major, though there has been absolutely no sign of trouble–the car sounds normal, runs fine, isn’t leaking anything, etc. They didn’t have (or weren’t offering) a free loaner, but did have an Enterprise rental desk in the showroom. I asked for a basic car to get me to and from work while mine is at the shop, and the car rental guy asked me, “As long as you pay the economy price, do you mind what you get?”

I parsed that sentence and didn’t see any danger, so I answered in the affirmative. What I got was a Mitsubishi Eclipse Spyder GTMitsubishi Eclipse Spyder GT. “And this is for the economy rate, right?” I asked, oh-so-casually.

“Yep.”

“Okay then.”

Wow. I haven’t enjoyed driving a car quite this much since I bought the Grand Prix itself six years ago. The weather today precluded lowering the top (and I’m not much for the windblown hair look anyway) but it hardly mattered. And I spotted the CHP car near SFO before he spotted me, so I was lucky there too.

I always wanted a 4-door for passenger and cargo capacity, but now that Jen has her Subaru Forester 2.5XS (4-door, tons of cargo space) I’m thinking something a little sportier might be nice. I don’t know that I’d go for the Eclipse itself–I’d really prefer an American-made car–but it is causing me to look in a different direction for my next car.

Leave a Comment

Countdown to IKEA… opening day

The Mercury News‘ Sheila Himmel explains IKEA dining options. You could eat all day at Ikea, and you may need to. The shortest path through the 290,000-square-foot store opening today in East Palo Alto runs more than half a mile. On average, customers spend more than two hours in Ikea stores, which means many people [...]

The Mercury News‘ Sheila Himmel explains IKEA dining options.

You could eat all day at Ikea, and you may need to. The shortest path through the 290,000-square-foot store opening today in East Palo Alto runs more than half a mile. On average, customers spend more than two hours in Ikea stores, which means many people stay a lot longer.

Wait a month and you can eat at Ikea 30 minutes before the store even opens for the day. On Oct. 1 the ground-floor bistro will begin serving breakfast starting at 9:30 a.m. every day but Friday and Saturday, when the gates to coffee and cinnamon buns open at 8:30 a.m.

The linked article also has a series of photos of the cafeteria and bistro areas of the new store.

Leave a Comment

Easy AdSense by Unreal

Project Insomnia is Digg proof thanks to caching by WP Super Cache