Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Created September 16, 2012 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iristyle/3732568 to your computer and use it in GitHub Desktop.
Save Iristyle/3732568 to your computer and use it in GitHub Desktop.
Update the Powershell environment variables based on system state
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