Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Created June 19, 2018 14:28
Show Gist options
  • Save JeremySkinner/d3a12fcb79ec834c25a32d9f22490781 to your computer and use it in GitHub Desktop.
Save JeremySkinner/d3a12fcb79ec834c25a32d9f22490781 to your computer and use it in GitHub Desktop.
Import env file
function Import-EnvFile {
$files = Get-ChildItem *.env
foreach($file in $files) {
write-host $file
$lines = Get-Content $file
foreach($line in $lines) {
if ($line -and !$line.StartsWith("#")) {
# Split by equals
$parts = $line.Split("=")
if ($parts.Length -eq 2) {
$name = $parts[0].Trim()
$value = $parts[1].Trim()
[Environment]::SetEnvironmentVariable($name, $value)
# If you want, you can set it user-wide (rather than just current shell)
# But that's much slower
# [Environment]::SetEnvironmentVariable($name, $value, 'User)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment