Setting Windows PowerShell environment variables

I found that setting the PATH environment variable only affects the old command prompt.PowerShell appears to have different environment settings.How do I change the environment variables for PowerShell (v1)?

Be careful:

I want the changes to take effect permanently, so I don't have to set them every time I run PowerShell.Does PowerShell have a configuration file?Like Bash profile on Unix?

#1st floor

If you need to temporarily add to the PATH environment variable at some point during a PowerShell session, you can do the following:

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

#2nd floor

You can also permanently modify user/system environment variables (that is, they will remain permanently after the Shell restarts) using the following commands:

Modify system environment variables

[Environment]::SetEnvironmentVariable
     ("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)

Modify user environment variables

[Environment]::SetEnvironmentVariable
     ("INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User)

Usage of Notes - Add to System Environment Variables

[Environment]::SetEnvironmentVariable(
    "Path",
    [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";C:\bin",
    [EnvironmentVariableTarget]::Machine)

If you don't want to write a type, you can also use a string-based solution

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\bin", "Machine")

#3rd floor

Although the currently accepted answer is in a sense feasible, that is, the path variable is permanently updated from the PowerShell context, it does not actually update the environment variables stored in the Windows registry.

To do this, you can obviously use PowerShell:

$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

$newPath=$oldPath+';C:\NewFolderToAddToTheList\'

Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath

For more information, see the blog post " Modify environment path using PowerShell"

If you use the PowerShell Community Extension, the correct command to add a path to an environment variable path is:

Add-PathVariable "C:\NewFolderToAddToTheList" -Target Machine

#4th floor

Most of the answers are not for UAC .This covers UAC issues.

First install the PowerShell Community Extension: through http://chocolatey.org/ choco install pscx (you may have to restart the Shell environment).

Then enable pscx

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser #allows scripts to run from the interwebs, such as pcsx

Then use Invoke-Elevated

Invoke-Elevated {Add-PathVariable $args[0] -Target Machine} -ArgumentList $MY_NEW_DIR

#5th floor

This sets the path of the current session and prompts the user to permanently add it:

function Set-Path {
    param([string]$x)
    $Env:Path+= ";" +  $x
    Write-Output $Env:Path
    $write = Read-Host 'Set PATH permanently ? (yes|no)'
    if ($write -eq "yes")
    {
        [Environment]::SetEnvironmentVariable("Path",$env:Path, [System.EnvironmentVariableTarget]::User)
        Write-Output 'PATH updated'
    }
}

You can add this function to the default configuration file (Microsoft.PowerShell_profile.ps1), which is usually located in%USERPROFILE%\Documents\WindowsPowerShell.

Tags: Session shell Unix Windows

Posted on Thu, 09 Jan 2020 17:55:55 -0800 by jerdo