-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathload-envFile.ps1
More file actions
146 lines (117 loc) · 4.56 KB
/
load-envFile.ps1
File metadata and controls
146 lines (117 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<#
.SYNOPSIS
Load environment variables from .env file into current PowerShell session.
.DESCRIPTION
Reads a .env file (default: .env in script root) and sets environment variables
for the current PowerShell session. Supports comments (#) and blank lines.
Does not override existing environment variables unless -Force is specified.
.PARAMETER Path
Path to the .env file. Defaults to .env in the script's directory.
.PARAMETER Force
Override existing environment variables with values from .env file.
.PARAMETER Scope
Scope for environment variables: Process (default), User, or Machine.
Process scope only affects current session.
.EXAMPLE
.\Load-EnvFile.ps1
Loads variables from .env file in current directory.
.EXAMPLE
.\Load-EnvFile.ps1 -Path "C:\config\.env.production" -Force
Loads variables from specific file, overwriting existing variables.
.EXAMPLE
. .\Load-EnvFile.ps1
Dot-source to load variables into calling scope.
.NOTES
Author: PowerShell Scripts Repository
Version: 1.0
Date: 2025-10-07
#>
[CmdletBinding()]
param(
[Parameter()]
[string]$Path = (Join-Path $PSScriptRoot ".env"),
[Parameter()]
[switch]$Force,
[Parameter()]
[ValidateSet('Process', 'User', 'Machine')]
[string]$Scope = 'Process'
)
function Load-EnvFile {
param(
[string]$EnvFilePath,
[bool]$OverrideExisting,
[string]$VariableScope
)
if (-not (Test-Path $EnvFilePath)) {
Write-Warning "Environment file not found: $EnvFilePath"
Write-Host "Create one by copying .env.example to .env and filling in your values." -ForegroundColor Yellow
return
}
Write-Host "Loading environment variables from: $EnvFilePath" -ForegroundColor Cyan
$loadedCount = 0
$skippedCount = 0
$loadedVars = @()
Get-Content $EnvFilePath | ForEach-Object {
$line = $_.Trim()
# Skip empty lines and comments
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith('#')) {
return
}
# Parse KEY=VALUE format
if ($line -match '^([^=]+)=(.*)$') {
$key = $matches[1].Trim()
$value = $matches[2].Trim()
# Remove quotes if present
if ($value -match '^["''](.*)["`'']$') {
$value = $matches[1]
}
$existingValue = [System.Environment]::GetEnvironmentVariable($key, $VariableScope)
if ($existingValue -and -not $OverrideExisting) {
Write-Verbose "Skipping $key (already set)"
$skippedCount++
}
else {
try {
# Only set if value is not empty
if (-not [string]::IsNullOrWhiteSpace($value)) {
[System.Environment]::SetEnvironmentVariable($key, $value, $VariableScope)
Write-Verbose "Set $key = $value"
# Mask sensitive values for display
$displayValue = $value
if ($key -match '(PASSWORD|SECRET|KEY|TOKEN)') {
$displayValue = "***MASKED***"
}
elseif ($value.Length -gt 50) {
$displayValue = $value.Substring(0, 47) + "..."
}
$loadedVars += [PSCustomObject]@{
Variable = $key
Value = $displayValue
}
$loadedCount++
}
}
catch {
Write-Warning "Failed to set $($key): $($_.Exception.Message)"
}
}
}
}
Write-Host "`n✓ Loaded $loadedCount environment variable(s)" -ForegroundColor Green
if ($skippedCount -gt 0) {
Write-Host " Skipped $skippedCount existing variable(s) (use -Force to override)" -ForegroundColor Yellow
}
# Display loaded variables with values
if ($loadedVars.Count -gt 0) {
Write-Host "`nPopulated Variables:" -ForegroundColor Cyan
$loadedVars | Format-Table -AutoSize | Out-String | ForEach-Object { Write-Host $_ -ForegroundColor Gray }
}
}
# Execute
try {
Load-EnvFile -EnvFilePath $Path -OverrideExisting $Force.IsPresent -VariableScope $Scope
}
catch {
Write-Error "Failed to load environment file: $($_.Exception.Message)"
exit 1
}