Private/Import-EnvFile.ps1

function Import-EnvFile {
    <#
    .SYNOPSIS
    Load environment variables from .env file
     
    .DESCRIPTION
    Reads a .env file and sets environment variables for the current session.
    Supports standard .env format with KEY=VALUE pairs.
     
    .PARAMETER Path
    Path to .env file. Defaults to .env in module root.
     
    .EXAMPLE
    Import-EnvFile
    Loads variables from default .env file
     
    .EXAMPLE
    Import-EnvFile -Path "C:\custom\.env"
    Loads variables from custom path
     
    .NOTES
    Author: waldo
    Version: 1.0.0
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false)]
        [string]$Path
    )
    
    begin {
        Write-Verbose "Starting $($MyInvocation.MyCommand.Name)"
    }
    
    process {
        try {
            # Default to module .env file
            if (-not $Path) {
                $Path = Join-Path $PSScriptRoot "../.env"
            }
            
            # Check if file exists
            if (-not (Test-Path $Path)) {
                Write-Verbose ".env file not found at: $Path"
                return
            }
            
            Write-Verbose "Loading environment variables from: $Path"
            
            # Read and parse .env file
            Get-Content $Path | ForEach-Object {
                $line = $_.Trim()
                
                # Skip empty lines and comments
                if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith('#')) {
                    return
                }
                
                # Parse KEY=VALUE
                if ($line -match '^([^=]+)=(.*)$') {
                    $key = $matches[1].Trim()
                    $value = $matches[2].Trim()
                    
                    # Only set if not already in environment (env vars take precedence)
                    if ([string]::IsNullOrWhiteSpace([Environment]::GetEnvironmentVariable($key))) {
                        [Environment]::SetEnvironmentVariable($key, $value)
                        Write-Verbose "Set environment variable: $key"
                    } else {
                        Write-Verbose "Skipped $key (already set in environment)"
                    }
                }
            }
            
            Write-Verbose "Environment variables loaded successfully"
        }
        catch {
            Write-Warning "Error loading .env file: $_"
        }
    }
    
    end {
        Write-Verbose "Completed $($MyInvocation.MyCommand.Name)"
    }
}