ModuleMembers.ps1

function Get-ModuleMembers {
<#
    .SYNOPSIS
        Finds all functions and aliases defined within a function. Intended to make it easier for defining / updating Export-ModuleMember paramters
    .DESCRIPTION
        Uses Get-Module and Select-String to find the functions and aliases defined within the various scripts of a PowerShell Module.
        Returns a custom objects enumerating these functions and aliases, showing a complete Export-ModuleMember statement to be optionally edited, and included at the end of a new or modified .psm1 file
    .EXAMPLE
        PS .\> Get-ModuleMembers -ModuleName EditModule
 
        ModuleName : EditModule
        Functions : Edit-Module
        Aliases :
        FunctionList :
        AliasList :
    .NOTES
        NAME : Get-ModuleMembers
        VERSION : 1.0.0
        LAST UPDATED: 2/16/2016
        AUTHOR : Bryan Dady
#>

    Param (
        [Parameter(Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = 'Specify a module (.psm1 file) to inspect'
        )]
        [ValidateScript({$PSItem -in (Get-Module -ListAvailable).Name})]
        [Alias('FilePath','Module','Path')]
        [string]
        $ModuleName
    )

    New-Variable -Name OutputObj -Description 'Object to be returned by this function' -Scope Private
    $thisModule = Get-Module -ListAvailable -Name $ModuleName

    Write-Debug -Message "`$thisModule = $($thisModule.Name), $($thisModule.Path)"

    $Functions = @()
    $Aliases   = @()

    $thisModule.ModuleBase | Get-ChildItem -Recurse -Include *.ps1,*.psm1 | Select-String -pattern '^\s*function (\S+)' | Group-Object -Property Filename | Select-Object -ExpandProperty Group |
        ForEach-Object -Process {
        Write-Debug -Message "Filename: $($PSItem.Filename); Match: $($PSItem.Matches.Groups[1].Value)"
            $Functions += $PSItem.Matches.Groups[1].Value
        } # end foreach Match

    $thisModule.ModuleBase | Get-ChildItem -Recurse -Include *.ps1,*.psm1 | Select-String -pattern '^(?:[^#\[]{0,7})?(?:New-|Set-)?Alias(?:\s*-Name)?\s*?(\S+)' | Group-Object -Property Filename,Matches | Select-Object -ExpandProperty Group |
        ForEach-Object -Process {
        Write-Output -InputObject $PSItem
        Write-Output -InputObject "Filename: $($PSItem.Filename); Match: $($PSItem.Matches.Groups[1].Value)"
            $Aliases += $PSItem.Matches.Groups[1].Value
        } # end foreach Match

    # Optimize New-Object invocation, per Don Jones' recommendation: https://technet.microsoft.com/en-us/magazine/hh750381.aspx
    $Private:properties = [ordered]@{
        'ModuleName'    = $($thisModule.Name | Select-Object -Unique)
        'ModulePath'    = $($thisModule.ModuleBase | Select-Object -Unique)
        'ModuleList'    = $($thisModule.ModuleList | Sort-Object | Select-Object -Unique)
        'RootModule'    = $($thisModule.RootModule | Select-Object -Unique)
        'Scripts'       = $($thisModule.Scripts | Sort-Object | Select-Object -Unique)
        'NestedModules' = $($thisModule.NestedModules | Sort-Object | Select-Object -Unique)
        'Functions'     = $($Functions | Sort-Object | Select-Object -Unique)
        'Aliases'       = $($Aliases | Sort-Object | Select-Object -Unique)
        'FunctionList'  = "'" + $(($Functions | Sort-Object | Select-Object -Unique) -join "', '") + "'"
        'AliasList'     = "'" + $(($Aliases | Sort-Object | Select-Object -Unique) -join "', '") + "'"
    }

    $Private:RetObject = New-Object -TypeName PSObject -Property $properties
    return $RetObject

} # end function Get-ModuleMembers