4

I've gathered and created quite a few Powershell functions that I use daily on my job. To make things easier to maintain and organized, I've created modules. Each module has its own folder with several files in it. Each file has one or more functions.

In the past, I've organized things as such:

\Modules\
   \SystemTools\
      Hotfixes.psm1
      Services.psm1
      SystemTools.psd1
   \NetworkTools\
      ActiveDirectory.psm1
      Connections.psm1
      NetworkTools.psd1

with Export-ModuleMember -Function in each psm1 file, an empty RootModule line in the manifest (psd1) file and all my psm1 files as an array on the NestedModules line in the manifest file.

But I'm not sure that's how it was intended to be used, and if it follows best practices regarding modules with several files.

So I've recently changed my Modules folder as such:

\Modules\
   \SystemTools\
      Hotfixes.ps1
      Services.ps1
      SystemTools.psd1
      SystemTools.psm1
   \NetworkTools\
      ActiveDirectory.ps1
      Connections.ps1
      NetworkTools.psd1
      NetworkTools.psm1

So I've

  1. renamed all psm1 files to ps1
  2. added a psm1 file where I dot source all ps1 files in the same folder
  3. set the psm1 file as RootModule and no NestedModules

Questions

  1. Both seem to work, but which one is better ?
  2. If a function defined in the SystemTools module needs to use a function defined in the NetworkTools module, should I use Import-Module ? Isn't there a risk of circular dependency ?
geoced
  • 693
  • 3
  • 16
  • This [blog post](https://ramblingcookiemonster.github.io/Building-A-PowerShell-Module/) by Warren Frame is something I've used for all my PowerShell modules. If you do not choose to pursue that method I at least implore you to create a separate directory for each module that is the name of the module. – Persistent13 Aug 21 '17 at 16:26
  • Personally I'd prefer the second approach. I've seen people argue against dot-sourcing the implementation scripts, but I have yet to see a compelling reason for that. – Ansgar Wiechers Aug 21 '17 at 16:52
  • All right, seems like the second version is indeed preferred by the community. What about my second question ? If a function in module A needs a function in module B, what's the best way to do it ? – geoced Aug 22 '17 at 08:50
  • [Maybe related](https://stackoverflow.com/q/5489494/1630171). My gut feeling says to use `Import-Module`, but I can't back that with facts. – Ansgar Wiechers Aug 23 '17 at 21:23
  • 2
    Thanks @AnsgarWiechers, it seems the best way (which would prevent circular dependencies) is to use `#Requires -Modules ModuleName` – geoced Aug 25 '17 at 07:45

0 Answers0