functions/Publish-MyModule.ps1

function Publish-MyModule
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]$Path,
        [string]$ApiKey = ''
    )

    #localdev pre ps7: [Environment]::SetEnvironmentVariable('NuGetApiKey', '[your-nuget-api-key]', 'User')
    #localdev ps7: Set-Item -Path Env:NuGetApiKey -Value '[your-nuget-api-key]'

    if ($Path -eq '')
    {
        $Path = ('{0}\..\' -f $PSScriptRoot )
    }

    [string]$CurrApiKey = ''
    if ($ApiKey -ne '')
        { $CurrApiKey = $ApiKey }
    if ($CurrApiKey -eq '')
        {$CurrApiKey = $env:PSGalleryApiKey }
    if ($CurrApiKey -eq '')
        {$CurrApiKey = $env:NuGetApiKey }

    $confirmation = Read-Host ("Do you want to publish '{0}' to PowerShell Gallery?" -f $Path)
    if ($confirmation -eq 'y')
    {
        try
        {
            # Build a splat containing the required details and make sure to stop for errors which will trigger the catch
            $params = @{
                Path        = ('{0}\..\' -f $PSScriptRoot )
                NuGetApiKey = $CurrApiKey
                ErrorAction = 'Stop'
            }
            
            Publish-Module @params
            Write-Output -InputObject ('PowerShell Module BcAdmin published to PowerShell Gallery')
        }

        catch
        {
            throw $_
        }
    }
}