private/Update-OSDeploySessionEnvironment.ps1
|
function Update-OSDeploySessionEnvironment { <# .SYNOPSIS Refreshes process environment variables from the Machine and User scopes .DESCRIPTION Reads environment variables from the Machine scope and then the User scope and writes them to the current PowerShell process. User-scope values replace Machine-scope values with the same name, except for Path. Rebuilds the process Path by joining the Machine Path and User Path with a semicolon. Empty Path values are omitted. The function changes only the current process environment and does not modify persistent Machine or User values. .EXAMPLE PS> Update-OSDeploySessionEnvironment Refreshes the current process environment after software changes persistent environment variables. .INPUTS None. This function does not accept pipeline input. .OUTPUTS None. .NOTES Author: David Segura Company: Recast Software Version: 1.0.0 Date: 2026-08-28 Change Summary: Refreshes the current process environment from persistent values. Requires Windows environment variable scopes supported by System.Environment. #> [CmdletBinding()] param () Write-Host "[$(Get-Date -Format s)] Refreshing session environment variables..." -ForegroundColor DarkGray # Machine scope first so User-scope values take precedence for non-Path vars foreach ($scope in @('Machine', 'User')) { $vars = [System.Environment]::GetEnvironmentVariables($scope) foreach ($key in $vars.Keys) { if ($key -ne 'Path') { [System.Environment]::SetEnvironmentVariable($key, $vars[$key], 'Process') } } } # Rebuild Path as Machine + User (matches standard Windows PATH merge behaviour) $machinePath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') $userPath = [System.Environment]::GetEnvironmentVariable('Path', 'User') $env:Path = (@($machinePath, $userPath) | Where-Object { -not [string]::IsNullOrEmpty($_) }) -join ';' Write-Host "[$(Get-Date -Format s)] Session environment variables refreshed." -ForegroundColor Green } |