-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Update-Environment.ps1
38 lines (33 loc) · 1022 Bytes
/
Update-Environment.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<#
.SYNOPSIS
Refresh the current session environment variables from the Registry by harvesting
from both the local machine and the current user hives.
.DESCRIPTION
Inspired by chocolatey.org's RefreshEnv.cmd script.
#>
Begin
{
function Harvest
{
param($regpath)
$path = ''
$values = Get-ItemProperty $regpath
$values | Get-Member -MemberType NoteProperty | Select -ExpandProperty Name | `
where { $_ -notmatch '^PS' -and $_ -ne 'USERNAME' -and $_ -ne 'PROCESSOR_ARCHITECTURE'} | `
foreach {
if ($_ -eq 'PATH') {
$path = $values.$_
} else {
Set-Item env:$_ "$($values.$_)"
}
}
$path
}
}
Process
{
Write-Host '... refreshing environment variables from registry'
$LMPath = Harvest 'HKLM:\\System\CurrentControlSet\Control\Session Manager\Environment'
$CUPath = Harvest 'HKCU:\\Environment'
Set-Item env:PATH "$LMPath;$CUPath"
}