Source/ConvertFrom-DotEnv.ps1

<#
.SYNOPSIS
    Converts .env-formatted text into a hashtable of key-value pairs.
.DESCRIPTION
    Parses .env-formatted text into a hashtable, one entry per KEY=VALUE line. Blank lines and # comments are ignored.
    Throws if a non-comment, non-blank line isn't in KEY=VALUE format.
.PARAMETER Text
    The .env-formatted text to convert.
.EXAMPLE
    $values = ConvertFrom-DotEnv -Text "KeyOne=One`nKeyTwo=Two"
.EXAMPLE
    Get-Content -Path ".env" -Raw | ConvertFrom-DotEnv
.INPUTS
    [System.String] The .env-formatted text. Accepts pipeline input by value or by property name.
.OUTPUTS
    [System.Collections.Hashtable] Every key-value pair found in the text.
#>


function ConvertFrom-DotEnv {

    [OutputType([hashtable])]
    [CmdletBinding()]

    param(
        [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [string]
        $Text
    )

    process {

        $values = @{}

        foreach ($line in ($Text -split "`r?`n")) {

            if ([string]::IsNullOrWhiteSpace($line) -or $line.Trim().StartsWith("#")) {

                continue

            }

            if ($line -notmatch '^\s*([^#][^=]+)=(.*)$') {

                throw "'$line' is not in KEY=VALUE format."

            }

            $values[$matches[1].Trim()] = $matches[2].Trim()

        }

        return $values

    }

}