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:

Or Get-Module:

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.

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

Know of a better way to retrieve a list of all of the private functions in a PowerShell module? I’d love to hear your thoughts or suggestions on this subject via a comment to this blog article.

µ

3 Comments

  1. mikeshepard

    I hadn’t thought of the .Invoke() method, but you can use the invoke operator (&) to get the same thing done…

    & $module {commands here}

    Reply
  2. Erik van Noorden

    Very nice post, exactly what i was looking for.
    I removed the compare from the code and used a Where-Object instead

    $module = Get-Module
    $publicFunctions = (Get-Command -Module $module.name).name
    $privateFunctions = ($module.Invoke({Get-Command -module $module.name})).name | Where-Object { $_ -notin $publicFunctions }

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: