48

I have a module xy which has several functions for the end user and several internal helper functions called by functions but not by the end user.

Get-Command -Module xy -CommandType function

lists all functions I have in my module folder (e.g. get-foo and get-foo_helper)

Is there a way to hide get-foo_helper function from the end user who is using:

Get-Command -Module tcaps -CommandType function

4 Answers 4

50

One thing I have done is use the verb-noun naming convention for functions I want to export, but leave out the hyphen in helper functions.

Then, export-modulemember *-* takes care of only exporting what you want to export.

Sign up to request clarification or add additional context in comments.

5 Comments

I also use this method. It is very easy to do, and the function names self document their visibility.
I am not going to down vote but I do think this is a bad idea as it violates the proper verb-noun naming conventions used by the rest of PowerShell even if the violation is only within your own module, though I do admire the pragmatic simplicity of your solution.
I completely understand your criticism. For me, the pragmatism outweighs the technical (private) violation.
@ChrisMagnuson: A mentor of mine had a great saying: "Rules are for fools and the guidance of wise men". In other words we shouldn't blindly follow rules or conventions but use them as useful guides. I think the verb-noun naming convention is a good example of this. We definitely should use it to name functions others will discover and use, to avoid breaking their expectations. However, if the functions aren't exposed to outside users there is no need to stick rigidly to the naming convention, especially if there is a benefit from not doing so. (BTW, not trying to imply anyone here is a fool)
@ChrisMagnuson, you could always use a different naming convention which fulfills the PowerShell best practices. E.g., have all public functions match a module prefix. Say I have "MyCoolModule" and have exported functions match: *-MCM*
39

Just add Export-ModuleMember to the bottom of you module.

Let's say you have the following Functions in your module:

New-Function0
New-Function1
New-Function2
New-HelperFunction0

Add these lines to the bottom of the module file:

Export-ModuleMember -function New-Function0
Export-ModuleMember -function New-Function1
Export-ModuleMember -function New-Function2

When you run Import-Module on this module, it will only import the functions defined by Export-ModuleMember.

Now let's say you also wanted to export an alias for New-Function1. just add this to the end of your module:

Export-ModuleMember -alias nf1

Now when you use Import-Module, it will load the functions you defined, as well as an alias (nf1) for New-Function1.

Comments

5

In many cases, a declared function can be replaced with a scriptblock (ie an anonymous function).

Comments

4

You can use Export-ModuleMember or create a Module Manifest and specify the exported commands. You can use New-ModuleManifest to create a manifest file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.