Functions/GenXdev.Windows/Update-Environment.ps1
|
<############################################################################## Part of PowerShell module : GenXdev.Windows Original cmdlet filename : Update-Environment.ps1 Original author : René Vaessen / GenXdev Version : 3.29.2026 ################################################################################ Copyright (c) 2026 René Vaessen / GenXdev Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ################################################################################> <# .SYNOPSIS Updates the environment variables in the current session. .DESCRIPTION Ensures that the environment variables are up to date in the current session. #> function Update-Environment { [CmdletBinding()] param () [string] $oldPath = "$($Env:PATH)" $null = & { [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::Machine).GetEnumerator() | Microsoft.PowerShell.Core\ForEach-Object -ErrorAction SilentlyContinue { try { Microsoft.PowerShell.Utility\Invoke-Expression "`$ENV:$($_.Key) = '$($_.Value.Replace('''', ''''''))'" -ErrorAction SilentlyContinue } catch {} } [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::User).GetEnumerator() | Microsoft.PowerShell.Core\ForEach-Object -ErrorAction SilentlyContinue { try { Microsoft.PowerShell.Utility\Invoke-Expression "`$ENV:$($_.Key) = '$($_.Value.Replace('''', ''''''))'" -ErrorAction SilentlyContinue } catch {} } } 1> $null 2>$null 3> $null 4> $null [string] $NewPath = "$($Env:PATH)" $Env:PATH = @( @( $NewPath.Split([System.IO.Path]::PathSeparator, [StringSplitOptions]::RemoveEmptyEntries) + $oldPath.Split([System.IO.Path]::PathSeparator, [StringSplitOptions]::RemoveEmptyEntries) ) | Microsoft.PowerShell.Core\ForEach-Object { GenXdev\Expand-Path "$_\" } | Microsoft.PowerShell.Utility\Select-Object -Unique ) -join [System.IO.Path]::PathSeparator } |