Return a List of the Private Functions in a PowerShell Module

In my quest to build a PowerShell module to convert a non-monolithic PowerShell module from development to a monolithic one for production, I wanted some way to validate that all of the functions were indeed migrated. While I'm pointing my tools to the public and private sub-folders within my development module and that should get them all, how can you be sure especially when a module may not have any private functions?

Determining the publicly accessible functions for a module is easy enough with either Get-Command:

1Get-Command -Module MrModuleBuildTools

list-private-functions1a.jpg

Or Get-Module:

1(Get-Module -Name MrModuleBuildTools).ExportedCommands.Values

list-private-functions2a.jpg

But what about getting a list of private functions? I tweeted this out and received a couple of responses for things I'd already tried, but that didn't work consistently. You could manually import the module's PSM1 file instead of its PSD1 file, but this could be inaccurate if Export-ModuleMember is used in the PSM1 file. Even if it isn't, if the module is non-monolithic and any of the PS1 files specify a Requires statement for other modules, those module's commands will show as being part of your module because it thinks they're nested modules.

The solution I found is that Get-Module has an Invoke method which can be used to run a command within the module's scope.

1$Module= Get-Module -Name MrModuleBuildTools
2$Module.Invoke({Get-Command -Module MrModuleBuildTools})

list-private-functions3a.jpg

Comparing the difference in the two returns a list of only the private functions.

1$All = $Module.Invoke({Get-Command -Module MrModuleBuildTools})
2$Exported = Get-Command -Module MrModuleBuildTools
3Compare-Object -ReferenceObject $All -DifferenceObject $Exported |
4Select-Object -ExpandProperty InputObject

list-private-functions4a.jpg

µ