Source/Set-DotEnv.ps1

<#
.SYNOPSIS
    Sets multiple key-value pairs in a .env file.
.DESCRIPTION
    Writes every key-value pair into the specified .env file.
    Replaces each key's value in place if it already exists, appends it if it doesn't, and creates the file if it doesn't exist yet.
    Accepts either a hashtable of key-value pairs, or the raw multi-line text of a .env file, converted via ConvertFrom-DotEnv.
    Never touches any environment variable.
.PARAMETER Values
    A hashtable of key-value pairs to write.
.PARAMETER Text
    The raw multi-line text of a .env file, as an alternative to passing -Values directly.
    Throws if a non-comment, non-blank line isn't in KEY=VALUE format.
.PARAMETER Path
    The path to the .env file. If not specified, writes to .env in the current directory.
.EXAMPLE
    Set-DotEnv -Values @{ Foo = "Bar"; Baz = "Qux" } -Path ".env"
.EXAMPLE
    Set-DotEnv -Text "Foo=Bar`nBaz=Qux" -Path ".env"
.EXAMPLE
    ".env", ".env.local" | Set-DotEnv -Values @{ Foo = "Bar" }
.INPUTS
    [System.String] The .env file path. Accepts pipeline input by value or by property name.
.OUTPUTS
    None.
#>


function Set-DotEnv {

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification = "ShouldProcess is handled by Set-DotEnvValue, which this function delegates every write to.")]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "ShouldProcess is handled by Set-DotEnvValue, which this function delegates every write to.")]
    [CmdletBinding(DefaultParameterSetName = "Object", SupportsShouldProcess)]

    param(
        [Parameter(Mandatory, ParameterSetName = "Object")]
        [hashtable]
        $Values,

        [Parameter(Mandatory, ParameterSetName = "Text")]
        [string]
        $Text,

        [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [string]
        $Path = ".env"
    )

    process {

        if ($PSCmdlet.ParameterSetName -eq "Text") {

            $Values = ConvertFrom-DotEnv -Text $Text

        }

        foreach ($name in $Values.Keys) {

            Set-DotEnvValue -Name $name -Value $Values[$name] -Path $Path -WhatIf:$WhatIfPreference -Confirm:$False

        }

    }

}