I creating a PowerShell script module using separate source files. What is the canonical way to reference source functions internal to the module from other internal source files?
For example if my module is created from PS source code in files "foo" and "bar"; and a function in "foo" needs to call a function in "bar", what is the best way to do that?
It doesn't seem like dot-sourcing would be a good idea. Nor does making the component files ("foo" and "bar") psm1 files. Is this the idea behind the "ScriptsToProcess" field in the psd1 file?
Am I thinking about this wrong (non-"PowerShelly")? Should I just dump everything into a single psm1?
Best Answer
I've personally followed the practice laid out by RamblingCookieMonster in his blog here: http://ramblingcookiemonster.github.io/Building-A-PowerShell-Module/
Which is to organise your functions in to separate .ps1 files under sub-folders
\Public
and\Private
. Public contains the functions the user should be able to call directly, Private is the functions internal to PowerShell.Then in the .psm1 file you load the functions via a loop and dot sourcing as follows:
You should then also explicitly list your Public function names in your .psd1 module manifest file under the
FunctionsToExport
setting. Doing this allows these functions to be discoverable and the module to be auto-loaded when they are used.