Source/ConvertTo-DotEnv.ps1
|
<# .SYNOPSIS Converts a hashtable of key-value pairs into .env-formatted text. .DESCRIPTION Serializes a hashtable into .env-formatted text, one KEY=VALUE line per entry. .PARAMETER Values A hashtable of key-value pairs to convert. .EXAMPLE ConvertTo-DotEnv -Values @{ KeyOne = "One"; KeyTwo = "Two" } .EXAMPLE @{ KeyOne = "One"; KeyTwo = "Two" } | ConvertTo-DotEnv | Set-Content -Path ".env" .INPUTS [System.Collections.Hashtable] The key-value pairs to convert. Accepts pipeline input by value or by property name. .OUTPUTS [System.String] The .env-formatted text. #> function ConvertTo-DotEnv { [OutputType([string])] [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [hashtable] $Values ) process { $lines = foreach ($name in $Values.Keys) { "$name=$($Values[$name])" } return ($lines -join "`n") } } |