Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I've 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?

share|improve this question
I'd like to have a central profile located on a file share. Synchronization is a pain. Creating a stub profile with . \\computer\share\path\Profile.ps1 seems like a kludge (try Notepad $Profile). It would be nice if there was a way to permanently change the $Profile automatic variable. – Nathan Hartley May 5 '11 at 14:09
1  
No the PATH environment does affect powershell command prompt as well. What differs though is that powershell does not accept paths enclosed in quotes. Solution: remove all enclosing quotes (") in the path environment variable – Nilzor Apr 16 at 11:12

5 Answers

up vote 96 down vote accepted

Changing the actual environment variables can be done by using the env: namespace / drive info. For example this code will update the path environment variable

$env:Path = "SomeRandomPath";

$env:Path = "SomeRandomPath";

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

c:\Users\JaredPar\Documents\WindowsPowerShell\profile.ps1
share|improve this answer
12  
$profile is an automatic variable that points at your user profile for all PowerShell hosts. – JasonMArcher Apr 3 '09 at 22:31
5  
Note that (split-path $profile)(to get the containing folder) can contain multiple profile files: profile.ps1 should be loaded by all hosts, <host-name>_profile.ps1 just by the specified host. For PowerShell.exe (console host), this is Microsoft.PowerShell_profile.ps1. – Richard Apr 4 '09 at 14:44

If you need to modify PATH environment variable temporarily, some time during PowerShell session, you can do it this way:

$env:Path = $env:Path + ";C:\Program Files\GnuWin32\bin"

Or even shorter as per Kevin's comment:

$env:Path += ";C:\Program Files\GnuWin32\bin"
share|improve this answer
thanks for the "+" trick, really helpful! – Stefano Apr 15 '11 at 12:10
23  
@Stefano, you can save a few more keystrokes with +=: $env:Path += ";c:\some\new\path" – Kevin Jun 3 '11 at 16:51
2  
+1 :: This one-liner is quite effective for session-based invocations as with mingw ... I.E. $env:PATH += ";c:\MinGW\msys\1.0\bin" ^ {some mingw bin ... } – Eddie B Feb 8 at 19:10

You can also modify user/system environment variables with the following ...

### Modify system environment variable ###
[Environment]::SetEnvironmentVariable
     ( "Path", $env:Path, [System.EnvironmentVariableTarget]::Machine )

### Modify user environment variable ###
[Environment]::SetEnvironmentVariable
     ( "INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User )
share|improve this answer
1  
This is permanent, right? – Andres Riofrio May 29 '12 at 3:52
This is a very useful for restricted access systems. – h0tw1r3 Sep 21 '12 at 17:57

From the PowerShell prompt:

setx PATH "$env:path;\the\directory\to\add" -m

you should then see the text:

SUCCESS: Specified value was saved.

restart your session and the variable will be available. setx can also be used to set arbitrary variables type setx /? 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

share|improve this answer
Seems to only work when 'running as administrator', and thereafter takes effect only for 'running as administrator' PowerShell consoles, not regularly running ones. – matt Feb 21 at 12:20
Here is some official Microsoft documentation for Setx. – Cupcake May 31 at 21:03
Ouch - just got hit by the 1024 character limit of setx; thankfully I followed the advice to record the existing value of $end:Path. Just something to be aware of: superuser.com/questions/387619/… – Jonno Aug 14 at 11:49

In PowerShell Community extensions (pscx) you have the following;

For guidance on installing pscx see this answer

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.