Unfortunately the other answers are missing some important details. PSModulePath has some special handling in PowerShell that needs to be considered when updating environment variables. Also, variables may be added, modified, or removed. And you need to take path environment variables into consideration.
To make it worse, depending on what product/host you are in when you do this, that product may have some special environment variable handling of its own that should be considered, and aside from hoping that Path environment variables (those that contain multiple values, semi-colon delimited) actually contain "Path" as part of their name, you may miss out on something (unless of course this is how Windows handles those environment variables internally, which could be, I didn't dig that far down the rabbit hole).
Here is a script that I came up with that is about as close as I think you'll get to properly updating environment variables in your current PowerShell session without restarting it:
# Get all environment variables for the current Process, as well as System and User environment variable values
$processValues = [Environment]::GetEnvironmentVariables('Process')
$machineValues = [Environment]::GetEnvironmentVariables('Machine')
$userValues = [Environment]::GetEnvironmentVariables('User')
# Identify the entire list of environment variable names first
$envVarNames = ($machineValues.Keys + $userValues.Keys + 'PSModulePath') | Sort-Object | Select-Object -Unique
# Now process all of those keys, updating what exists and adding what is new
foreach ($envVarName in $envVarNames) {
if ($envVarName -eq 'PSModulePath') {
$pieces = @()
if ($PSVersionTable.PSVersion -ge [System.Version]'4.0') {
$pieces += Join-Path -Path ${env:ProgramFiles} -ChildPath 'WindowsPowerShell\Modules'
}
if (-not $userValues.ContainsKey($envVarName)) {
$pieces += Join-Path -Path ([Environment]::GetFolderPath('Documents')) -ChildPath 'WindowsPowerShell\Modules'
} else {
$pieces += $userValues[$envVarName] -split ';'
}
if ($machineValues.ContainsKey($envVarName)) {
$pieces += $machineValues[$envVarName] -split ';'
}
[Environment]::SetEnvironmentVariable($envVarName,($pieces -join ';'),'Process')
} elseif ($envVarName -match 'path') {
$pieces = @()
if ($userValues.ContainsKey($envVarName)) {
$pieces += $userValues[$envVarName] -split ';'
}
if ($machineValues.ContainsKey($envVarName)) {
$pieces += $machineValues[$envVarName] -split ';'
}
[Environment]::SetEnvironmentVariable($envVarName,($pieces -join ';'),'Process')
} elseif ($userValues.ContainsKey($envVarName)) {
[Environment]::SetEnvironmentVariable($envVarName,$userValue[$envVarName],'Process')
} elseif ($machineValues.ContainsKey($envVarName)) {
[Environment]::SetEnvironmentVariable($envVarName,$machineValue[$envVarName],'Process')
}
}
# Lastly remove the environment variables that no longer exist
foreach ($envVarName in $processValues.Keys | Where-Object {$envVarNames -notcontains $_}) {
Remove-Item -LiteralPath "env:${envVarName}"
}
Note that this is largely untested however the principle is sound and it is based on work I have done in the past in this area.