Setting Windows PowerShell environment variables
If, some time during a PowerShell session, you need to see or to temporarily modify the PATH environment variable , you can type one of these commands:
$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end
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"; (replaces existing path)
$env:Path += ";SomeRandomPath" (appends to existing path)
Making change permanent
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 Powershell profiles script.
Everytime a new instance of Powershell starts, it look for specific script files (named profile files) and execute them if they do exist. You can edit one of these profile to customize your enviroment.
To know where those profile scripts are located in your computer type:
$profile
$profile.AllUsersAllHosts
$profile.AllUsersCurrentHost
$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
You can edit one of them, for example, by typing:
notepad $profile
You can also modify user/system environment variables permanently (i.e. will be persistent across shell restarts) with the following:
Modify a system environment variable
[Environment]::SetEnvironmentVariable
("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
Modify a user environment variable
[Environment]::SetEnvironmentVariable
("INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User)
Usage from comments - add to the system environment variable
[Environment]::SetEnvironmentVariable(
"Path",
[Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";C:\bin",
[EnvironmentVariableTarget]::Machine)
String based solution is also possible if you don't want to write types
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\bin", "Machine")