Created
November 6, 2014 02:45
-
-
Save swestner/b56b8d361b6974019556 to your computer and use it in GitHub Desktop.
Update Environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-EnvironmentVariableNames([System.EnvironmentVariableTarget] $Scope) { | |
switch ($Scope) { | |
'User' { Get-Item 'HKCU:\Environment' | Select-Object -ExpandProperty Property } | |
'Machine' { Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | Select-Object -ExpandProperty Property } | |
'Process' { Get-ChildItem Env:\ | Select-Object -ExpandProperty Key } | |
default { throw "Unsupported environment scope: $Scope" } | |
} | |
} | |
function Get-EnvironmentVariable([string] $Name, [System.EnvironmentVariableTarget] $Scope) { | |
[Environment]::GetEnvironmentVariable($Name, $Scope) | |
} | |
Write-Host "Running 'Update-SessionEnvironment' - Updating the environment variables for the session." | |
#ordering is important here, $user comes after so we can override $machine | |
'Machine', 'User' | | |
% { | |
$scope = $_ | |
Get-EnvironmentVariableNames -Scope $scope | | |
% { | |
Set-Item "Env:$($_)" -Value (Get-EnvironmentVariable -Scope $scope -Name $_) | |
} | |
} | |
#Path gets special treatment b/c it munges the two together | |
$paths = 'Machine', 'User' | | |
% { | |
(Get-EnvironmentVariable -Name 'PATH' -Scope $_) -split ';' | |
} | | |
Select -Unique | |
$Env:PATH = $paths -join ';' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment