Setting Windows PowerShell environment variables Setting Windows PowerShell environment variables powershell powershell

Setting Windows PowerShell environment variables


If, some time during a PowerShell session, you need toappend to the PATH environment variable temporarily, you cando it this way:

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


Changing the actual environment variables can be done byusing the env: namespace / drive information. For example, thiscode will update the path environment variable:

$env:Path = "SomeRandomPath";             (replaces existing path) $env:Path += ";SomeRandomPath"            (appends to existing path)

There are ways to make environment settings permanent, butif you are only using them from PowerShell, it's probablya lot better to use your profile to initiate thesettings. On startup, PowerShell will run any .ps1files it finds in the WindowsPowerShell directory underMy Documents folder. Typically you have a profile.ps1file already there. The path on my computer is

C:\Users\JaredPar\Documents\WindowsPowerShell\profile.ps1


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")