From f18c456419ec8f35ae0a2dbe409b4d2bd038458a Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Mon, 16 Jun 2025 09:58:31 +0300 Subject: [PATCH] Features: 1) Add collection of environment variables into a hashtable for improved handling; 2) Format and display all loaded environment variables as a single string output after processing; Fixes: 1) Suppress unnecessary output from New-Item calls to improve script clarity; Extra: 1) Minor refactor to ensure sorted and formatted output of environment variables; 2) Add consistent structure and efficiency improvements to the script; --- scripts/Windows/export-environment-file.ps1 | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/scripts/Windows/export-environment-file.ps1 b/scripts/Windows/export-environment-file.ps1 index c384c7d3..41f88480 100644 --- a/scripts/Windows/export-environment-file.ps1 +++ b/scripts/Windows/export-environment-file.ps1 @@ -10,13 +10,14 @@ if (-not (Test-Path $envFile)) exit 1 } +$envVars = @{ } + Get-Content $envFile | ForEach-Object { $line = $_.Trim() if ($line -eq '' -or $line.StartsWith('#')) { return } - if ($line -notmatch '=') { return @@ -37,9 +38,14 @@ Get-Content $envFile | ForEach-Object { $value = $value.Substring(1, $value.Length - 2) } - New-Item -Path Env:\$key -Value $value - - Write-Host "Set environment variable: $key" + New-Item -Path Env:\$key -Value $value | Out-Null + $envVars[$key] = $value } -Write-Host "All variables loaded from $envFile." +if ($envVars.Count -gt 0) +{ + $formatted = ($envVars.GetEnumerator() | Sort-Object Name | ForEach-Object { + "$( $_.Key )=$( $_.Value )" + }) -join ';' + Write-Host $formatted +} \ No newline at end of file