DEV Community

Gianluigi Disconzi
Gianluigi Disconzi

Posted on

Configure Path variable in Powershell

Scenario: for whatever reason you have to develop on a Windows machine and you're not an administrator. Your company enforces the same old Java version on all machines, but you want to try out a more recent one.
After you download a JDK binary distribution in a folder of your choice, it's really easy to set up your IDE to work with that, but what if you want to run Java from Powershell?
Typically your machine will be set up with a system Path variable which points to the old Java version, and that cannot be changed. You try to change the Path in your user variables adding the location to your brand newly downloaded JDK. Hooray right?

Well... no 😥

It turns out that user Paths are postponed to system Paths, and when looking for an executable Windows picks the first match.

Luckily for us, it turns out there's a workaround for that! Similarly to bash with its .bashrc file, Powershell supports init scripts.
To create an init script, run this command from Powershell:

New-Item $profile -Type File -Force

Go to your Documents folder, you'll find a new WindowsPowerShell subfolder which contains a Powershell script named Microsoft.PowerShell_profile.ps1. Edit that file and add a line like this:

 $Env:Path="C:\Users\my-user-name\apps\my-jdk-folder\bin;"+$Env:Path

Save and open a new Powershell instance: the script will run automatically when a new shell instance is created. Now you can see the new Path variable with the following command:

echo $env:Path

Top comments (0)