3

I'm new to modules and want to split up a lengthy .psm1 file into multiple files for maintainability. What is the best way to do this?

I've got it working using dot sourcing but I suspect there is a better way of creating a module with multiple files.

Currently my file structure is:

Logger/ (module directory)
|
-- Logger.psd1 (module manifest)
|
-- Logger.psm1 (main functions exported from module)
|
-- Configuration.ps1 (configuration-related functions, exported from module)
|
-- Private/ (directory)
    |
    -- SharedFunctions.ps1 (functions called by Logger.psm1 and 
    |                       Configuration.ps1, not exported)
    |
    -- ModuleState.ps1 (variables that record the module configuration, 
                        used by all other script files)

I've got this working by chaining the files together using dot sourcing:

  • In Logger.psm1 I've added . $PSScriptRoot\Configuration.ps1 to include the functions from the Configuration.ps1 file;
  • In Configuration.ps1 I've added . $PSScriptRoot\Private\SharedFunctions.ps1 to include the functions from Private\SharedFunctions.ps1;
  • In SharedFunctions.ps1 I've added . $PSScriptRoot\ModuleState.ps1 to include the variables in ModuleState.ps1 which record the module configuration.

This works but seems brittle and messy. Is there a better way to split files within a module, perhaps using the manifest file?

Simon Elms
  • 17,832
  • 21
  • 87
  • 103

0 Answers0