There’s no need to go dumpster diving in the registry for this stuff:
foreach($level in "Machine","User") {
[Environment]::GetEnvironmentVariables($level)
}
If you want to make PATH variables work right, you’d have to treat them specially (that is, concatenate the (potentially) new stuff to what you already have, and not lose anything, because you can’t tell for sure what you should remove).
foreach($level in "Machine","User") {
[Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
# For Path variables, append the new values, if they're not already in there
if($_.Name -match 'Path$') {
$_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
}
$_
} | Set-Content -Path { "Env:$($_.Name)" }
}
Notice that Set-Content actually sets the variables in your process environment, the same as doing something like $env:Temp = Convert-Path ~\AppData\Local\Temp