[Windows] Refactor PATH helpers (#8885)

This commit is contained in:
Vasilii Polikarpov
2023-11-29 13:00:16 +01:00
committed by GitHub
parent c73276d3f6
commit bfe32a2b12
14 changed files with 179 additions and 191 deletions

View File

@@ -869,3 +869,39 @@ function Test-FileSignature {
throw "Signature thumbprint do not match expected."
}
}
function Update-Environment {
<#
.SYNOPSIS
Updates the environment variables by reading values from the registry.
.DESCRIPTION
This function updates current environment by reading values from the registry.
It is useful when you need to update the environment variables without restarting the current session.
.NOTES
The function requires administrative privileges to modify the system registry.
#>
$locations = @(
'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
'HKCU:\Environment'
)
# Update PATH variable
$pathItems = $locations | ForEach-Object {
(Get-Item $_).GetValue('PATH').Split(';')
} | Select-Object -Unique
$Env:PATH = $pathItems -join ';'
# Update other variables
$locations | ForEach-Object {
$key = Get-Item $_
foreach ($name in $key.GetValueNames()) {
$value = $key.GetValue($name)
if (-not ($name -ieq 'PATH')) {
Set-Item -Path Env:$name -Value $value
}
}
}
}