I have found out that setting the PATH environment variable affects only the old command prompt. PowerShell seems to have different environment settings. How do I change the environment variables for PowerShell (v1)?
Note:
I want to make my changes permanent, so I don't have to set it every time I run PowerShell. Does PowerShell have a profile file? Something like Bash profile on Unix?
Most answers aren't addressing UAC. This covers UAC issues.
First install PowerShell Community Extensions:
choco install pscx
via http://chocolatey.org/ (you may have to restart your shell environment).Then enable pscx
Then use
Invoke-Elevated
This sets the path for the current session and prompts the user to add it permanently:
You can add this function to your default profile, (
Microsoft.PowerShell_profile.ps1
), usually located at%USERPROFILE%\Documents\WindowsPowerShell
.If, some time during a PowerShell session, you need to append to the PATH environment variable temporarily, you can do it this way:
Changing the actual environment variables can be done by using the
env: namespace / drive
information. For example, this code will update the path environment variable:There are ways to make environment settings permanent, but if you are only using them from PowerShell, it's probably a lot better to use your profile to initiate the settings. On startup, PowerShell will run any .ps1 files it finds in the WindowsPowerShell directory under My Documents folder. Typically you have a profile.ps1 file already there. The path on my computer is
From the PowerShell prompt:
You should then see the text:
Restart your session, and the variable will be available.
setx
can also be used to set arbitrary variables. Typesetx /?
at the prompt for documentation.Before messing with your path in this way, make sure that you save a copy of your existing path by doing
$env:path >> a.out
in a PowerShell prompt.As Jonathan Leaders mentioned here, it is important to run the command/script elevated to be able to change environment variables for 'machine', but running some commands elevated doesn't have to be done with the Community Extensions, so I'd like to modify and extend JeanT's answer in a way, that changing machine variables also can be performed even if the script itself isn't run elevated: