Structuring PowerShell Modules

suggest change

Rather than defining every function in one .psm1 file, you can put functions in separate files and dot-source those files from the script module. Dot-sourcing runs them in the module's scope.

Export the public functions with Export-ModuleMember, or list them in the manifest's FunctionsToExport. Otherwise callers may not see them.

Consider this module directory:

MyCoolModule\
  Functions\
    Function1.ps1
    Function2.ps1
    Function3.ps1
  MyCoolModule.psd1
  MyCoolModule.psm1

Inside your MyCoolModule.psm1 file, you could insert the following code:

Get-ChildItem -Path $PSScriptRoot\Functions | 
  ForEach-Object -Process { . $PSItem.FullName }

This would dot-source the individual function files into the .psm1 module file.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents