Source/Get-DotEnv.ps1
|
<# .SYNOPSIS Reads a .env file and returns its key-value pairs. .DESCRIPTION Reads the specified .env file and converts it via ConvertFrom-DotEnv, without setting any environment variable. Throws if the file is not found or isn't well-formed. .PARAMETER Path The path to the .env file. If not specified, reads .env in the current directory. .EXAMPLE $values = Get-DotEnv -Path ".env" .EXAMPLE $values = ".env", ".env.local" | Get-DotEnv .INPUTS [System.String] The .env file path. Accepts pipeline input by value or by property name. .OUTPUTS [System.Collections.Hashtable] Every key-value pair in the file. #> function Get-DotEnv { [OutputType([hashtable])] [CmdletBinding()] param( [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Path = ".env" ) process { if (-not (Test-Path -Path $Path)) { throw "$Path file not found." } return ConvertFrom-DotEnv -Text (Get-Content -Path $Path -Raw) } } |