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
- renamed all psm1 files to ps1
- added a psm1 file where I dot source all ps1 files in the same folder
- set the psm1 file as
RootModule
and noNestedModules
Questions
- Both seem to work, but which one is better ?
- 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 ?