Public/Get-PSModuleCommandPreview.ps1

function Get-PSModuleCommandPreview {
    <#
    .SYNOPSIS
    Previews the commands exported by a module.
    .DESCRIPTION
    If the module is installed locally, returns the actual exported commands.
    If not installed, returns metadata-based command names from the Gallery.
    Does not execute arbitrary code or perform unsafe operations.
    .PARAMETER Name
    The module name.
    .PARAMETER Repository
    The PowerShell repository to query. Defaults to 'PSGallery'.
    .EXAMPLE
    Get-PSModuleCommandPreview -Name "Pester"
    #>

    [CmdletBinding()]
    [OutputType([string[]])]
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [string]$Repository = 'PSGallery'
    )

    # Check local installation first
    $localModule = Get-InstalledModuleInfo -Name $Name

    if ($localModule) {
        return @($localModule.ExportedCommands.Keys | Sort-Object)
    }

    # Fall back to gallery metadata
    try {
        $info = Invoke-ProviderGetInfo -Name $Name -Repository $Repository
        if ($info -and $info.ExportedCommands) {
            return @($info.ExportedCommands | Sort-Object)
        }
        return @()
    } catch {
        $ErrorActionPreference = 'Continue'
        Write-Error "Get-PSModuleCommandPreview failed for '$Name': $_"
        return @()
    }
}