Skip to content

Instantly share code, notes, and snippets.

@smerle33
Created May 27, 2025 15:33
Show Gist options
  • Save smerle33/8663f17877211c43627c328ffe9616c4 to your computer and use it in GitHub Desktop.
Save smerle33/8663f17877211c43627c328ffe9616c4 to your computer and use it in GitHub Desktop.
script powershell to create env vars from .env file
# C:> ./DotEnvToEnVars.ps1 ./myproject/.env -Verbose
param(
[string]$Path,
[switch]$Verbose,
[switch]$Remove,
[switch]$RemoveQuotes
)
$variables = Select-String -Path $Path -Pattern '^\s*[^\s=#]+=[^\s]+$' -Raw
foreach($var in $variables) {
$keyVal = $var -split '=', 2
$key = $keyVal[0].Trim()
$val = $RemoveQuotes ? $keyVal[1].Trim("'").Trim('"') : $keyVal[1]
[Environment]::SetEnvironmentVariable($key, $Remove ? '' : $val)
if ($Verbose) {
"$key=$([Environment]::GetEnvironmentVariable($key))"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment