Created
September 16, 2012 14:02
-
-
Save Iristyle/3732568 to your computer and use it in GitHub Desktop.
Update the Powershell environment variables based on system state
This file contains hidden or 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 Update-SessionEnvironment { | |
$user = 'HKCU:\Environment' | |
$machine ='HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | |
#ordering is important here, $user comes after so we can override $machine | |
$machine, $user | | |
Get-Item | | |
% { | |
$regPath = $_.PSPath | |
$_ | | |
Select -ExpandProperty Property | | |
% { | |
Set-Item "Env:$($_)" -Value (Get-ItemProperty $regPath -Name $_).$_ | |
} | |
} | |
#Path gets special treatment b/c it munges the two together | |
$paths = 'Machine', 'User' | | |
% { | |
[Environment]::GetEnvironmentVariable('PATH', $_) -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