Templates/Config.ps1

#region optional module settings (never writes into the module folder)
# Loads module-scoped variables from Config.psd1 in the user's settings folder for this module
# (<settings root>/<ModuleName>/Config.psd1, see Get-ModuleConfig). The module folder is never written to,
# so this works for modules installed for all users. Create the file yourself, for example:
# @{ ApiBaseUri = 'https://api.example.com' }
# Do not put secrets in Config.psd1. Save them encrypted with Set-ModuleSecret and read them where they are
# needed with Get-ModuleSecret (tcs.core 0.4.0 or later), for example:
# Set-ModuleSecret -ModuleName '###TEMPLATE_MODULE_NAME' -Name 'ApiToken' -SecureString (Read-Host -AsSecureString -Prompt 'API token')
# $ApiToken = Get-ModuleSecret -ModuleName '###TEMPLATE_MODULE_NAME' -Name 'ApiToken'
try {
    $ModuleSettings = Get-ModuleConfig -CommandPath $PSCommandPath -ErrorAction Stop
    $ConfigFilePath = Join-Path -Path $ModuleSettings.ModuleConfigPath -ChildPath 'Config.psd1'
    if (Test-Path -Path $ConfigFilePath -PathType Leaf) {
        $ModuleVariables = Import-PowerShellDataFile -Path $ConfigFilePath -ErrorAction Stop
        foreach ($VariableName in $ModuleVariables.Keys) {
            Write-Verbose "Setting module variable '$VariableName' from '$ConfigFilePath'."
            Set-Variable -Name $VariableName -Value $ModuleVariables[$VariableName] -Scope Script -Force
        }
    }
    else {
        Write-Verbose "No module settings file at '$ConfigFilePath'; module variables were not loaded."
    }
}
catch {
    Write-Warning "Module settings could not be loaded: $($_.Exception.Message)"
}
#endregion