Setting Windows PowerShell environment variables

前端 未结 18 1911
日久生厌
日久生厌 2020-11-22 11:03

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

18条回答
  •  遥遥无期
    2020-11-22 11:44

    I tried to optimise SBF's and Michael's code a bit to make it more compact.

    I am relying on PowerShell's type coercion where it automatically converts strings to enum values, so I didn't define the lookup dictionary.

    I also pulled out the block that adds the new path to the list based on a condition, so that work is done once and stored in a variable for re-use.

    It is then applied permanently or just to the Session depending on the $PathContainer parameter.

    We can put the block of code in a function or a ps1 file that we call directly from the command prompt. I went with DevEnvAddPath.ps1.

    param(
        [Parameter(Position=0,Mandatory=$true)][String]$PathChange,
    
        [ValidateSet('Machine', 'User', 'Session')]
        [Parameter(Position=1,Mandatory=$false)][String]$PathContainer='Session',
        [Parameter(Position=2,Mandatory=$false)][Boolean]$PathPrepend=$false
    )
    
    [String]$ConstructedEnvPath = switch ($PathContainer) { "Session"{${env:Path};} default{[Environment]::GetEnvironmentVariable('Path', $containerType);} };
    $PathPersisted = $ConstructedEnvPath -split ';';
    
    if ($PathPersisted -notcontains $PathChange) {
        $PathPersisted = $(switch ($PathPrepend) { $true{,$PathChange + $PathPersisted;} default{$PathPersisted + $PathChange;} }) | Where-Object { $_ };
    
        $ConstructedEnvPath = $PathPersisted -join ";";
    }
    
    if ($PathContainer -ne 'Session') 
    {
        # Save permanently to Machine, User
        [Environment]::SetEnvironmentVariable("Path", $ConstructedEnvPath, $PathContainer);
    }
    
    # Update the current session
    ${env:Path} = $ConstructedEnvPath;
    

    I do something similar for a DevEnvRemovePath.ps1.

    param(
        [Parameter(Position=0,Mandatory=$true)][String]$PathChange,
    
        [ValidateSet('Machine', 'User', 'Session')]
        [Parameter(Position=1,Mandatory=$false)][String]$PathContainer='Session'
    )
    
    [String]$ConstructedEnvPath = switch ($PathContainer) { "Session"{${env:Path};} default{[Environment]::GetEnvironmentVariable('Path', $containerType);} };
    $PathPersisted = $ConstructedEnvPath -split ';';
    
    if ($PathPersisted -contains $PathChange) {
        $PathPersisted = $PathPersisted | Where-Object { $_ -ne $PathChange };
    
        $ConstructedEnvPath = $PathPersisted -join ";";
    }
    
    if ($PathContainer -ne 'Session') 
    {
        # Save permanently to Machine, User
        [Environment]::SetEnvironmentVariable("Path", $ConstructedEnvPath, $PathContainer);
    }
    
    # Update the current session
    ${env:Path} = $ConstructedEnvPath;
    

    So far, they seem to work.

提交回复
热议问题