Refresh your Windows Shell Environment with One Command to Access...

by Michael Szul on

No ads, no tracking, and no data collection. Enjoy this article? Buy us a ☕.

You know what's a pain? Installing a new program or utility from the command line and then having to close out your command prompt or PowerShell window only to open a new one to access changes to path environment variables.

For example, let's say you've installed Chocolatey, which means you now have access to cinst from the command line. You drop into a command prompt and decide to install nodeJS, so you type cinst node.install. At this point nodeJS and npm are now installed, but if you immediately decide to install something with npm install, you won't have access to npm from the command line because even though the Windows path variable has been updated, the current shell environment is still running off of the older shell environment. This might be a minor inconvenience, but consider your DevOps flow.

I like to use Boxstarter to create scripts for setting up new machines or virtual environments. Boxstarter uses Chocolatey, which is a Windows package manager. Chocolately is a command line utility, so if you want to install NodeJS, Bower, Yeoman, etc. all in one shot, you'll need to refresh your environment. Furthermore, if you're running it all from a Boxstarter script, you might have Chocolatey-specific commands, npm packages, NuGet packages, etc. that all have prerequisites, but also need to be in the path in order to run.

For my initial Boxstarter scripts, the npm modules were failing, and I suspected the path was the issue, so I contacted Boxstarter programmer Matt Wrock to confirm:

Rob Reynolds then chimed in with this nice gem:

As it turns out, if you have Chocolatey installed, you have command prompt and PowerShell access to a refreshenv command that will refresh your environment variables, including the path. Adding this command to your Boxstarter scripts means that you can set up entire tooling chains without worrying about the "freshness" of your shell environment.