Source/Import-DotEnvValue.ps1

<#
.SYNOPSIS
    Imports a single value from a .env file as a Process-scope environment variable.
.DESCRIPTION
    Reads the specified .env file and sets the given key as a process-scope environment variable.
    Throws if the file or the key is not found.
.PARAMETER Name
    The key to read and import.
.PARAMETER Path
    The path to the .env file. If not specified, reads .env in the current directory.
.EXAMPLE
    Import-DotEnvValue -Name "Foo" -Path ".env"
.EXAMPLE
    "Foo", "Bar" | Import-DotEnvValue -Path ".env"
.INPUTS
    [System.String] The key to read and import. Accepts pipeline input by value or by property name.
.OUTPUTS
    None.
#>


function Import-DotEnvValue {

    [CmdletBinding(SupportsShouldProcess)]

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

        [string]
        $Path = ".env"
    )

    begin {

        $values = Get-DotEnv -Path $Path

    }

    process {

        if (-not $values.ContainsKey($Name)) {

            throw "Key '$Name' not found in '$Path'."

        }

        if ($PSCmdlet.ShouldProcess($Name, "Set environment variable from '$Path' (Process)")) {

            [Environment]::SetEnvironmentVariable($Name, $values[$Name], "Process")

        }

    }

}