Setting Windows PowerShell path variable
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?
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:
$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:UsersJaredParDocumentsWindowsPowerShellprofile.ps1
如果在PowerShell会话期间需要暂时修改PATH环境变量,可以这样做:
$env:Path += ";C:Program FilesGnuWin32bin"
您也可以使用以下方式永久修改用户/系统环境变量(即在整个shell重启过程中持久化)
### Modify system environment variable ###
[Environment]::SetEnvironmentVariable
( "Path", $env:Path, [System.EnvironmentVariableTarget]::Machine )
### Modify user environment variable ###
[Environment]::SetEnvironmentVariable
( "INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User )
### from comments ###
### Usage from comments - Add to the system environment variable ###
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:bin", [EnvironmentVariableTarget]::Machine)
链接地址: http://www.djcxy.com/p/29094.html
上一篇: 我如何只使用Get获取目录