Private/New-Psm1File.ps1

function New-Psm1File {
    <#
    .SYNOPSIS
        Creates a PowerShell module loader file (.psm1).
 
    .DESCRIPTION
        This function generates a .psm1 file that acts as the entry point for a PowerShell module.
        It includes code to dynamically load functions defined in the module's 'Public' and 'Private' subfolders.
 
    .PARAMETER name
        Required. The name of the PowerShell module.
 
    .PARAMETER path
        Required. The path where the .psm1 file will be created.
 
    .EXAMPLE
        New-Psm1File -name MyModule -path C:\MyProject\Modules
        This creates a file named "MyModule.psm1" in the "C:\MyProject\Modules" directory.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0.0 - Initial release
    #>


    [cmdletbinding()]

    param (
        [parameter (mandatory = $true)]
        [string]$name,

        [parameter (mandatory = $true)]
        [string]$path
    )

    begin {

        # .psm1 module loader code
        $moduleLoaderCode = @'
 #Get public and private function definition files.
$Public = @(Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue)
$Private = @(Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue)
 
#Dot source the files
Foreach($import in @($Public + $Private)) {
    Try {
        . $import.fullname
    } Catch {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}
 
Export-ModuleMember -Function $Public.Basename
'@

    }

    process {
        # Create .psm1 module loader
        Write-Host "Creating PSM1 file $(Join-Path "$Path" "$name.psm1") " -ForegroundColor DarkCyan

        try {
            if ((Test-Path (Join-Path $Path "$name.psm1")) ) {
                Write-Host "Psm1 file already exists" -ForegroundColor DarkYellow
            } else {
                New-Item -Path "$Path" -Name "$name.psm1" -ItemType File | Out-Null -ErrorAction stop -ev x
                Set-Content -Path (Join-Path "$Path" "$name.psm1") -Value $moduleLoaderCode
                Write-Host "Psm1 file created successfully" -ForegroundColor darkGreen
            }
        } catch {
            Write-Warning "Unable to create $name.psm1 file $_"
        }
    }
}