A PSM1 file is a PowerShell script module: the module entry point that implements functions directly or loads them from supporting scripts. It turns related automation functions into a reusable unit that can be imported rather than copied into each operational script. section 2.2.2
A minimal module can consist of one .psm1 file containing its functions. For maintainability, pair it with a PSD1 module manifest: the manifest supplies metadata, including a version, and controls which functions or variables are public. The module folder, PSD1 file, and PSM1 file use the same module name so Import-Module can locate the module by name. section 2.2.2 section 2.2.3
Use the PSM1 file as the module’s loading and composition layer:
- Keep a small module in one PSM1 file when its functions remain easy to test and version. section 2.2.2
- Split larger modules into individual .ps1 function files, then have the PSM1 discover and dot-source them. section 2.2.2
- Organize callable functions under Public and helper functions under Private; folder names aid organization, while the manifest determines actual exports. section 2.2.3
- Update the manifest whenever a new public function is added, rather than exporting every loaded function with a wildcard. section 2.2.3
File cleanup module
A FileCleanupTools module can load archive-path and cleanup functions from a Public folder. A separate automation script imports FileCleanupTools, finds aged log files, creates a dated archive path, compresses the files, and removes the archived originals. The reusable behavior lives behind the module boundary; the operational script supplies paths, retention days, and execution flow. section 2.2.2
Listing 2.7 Loading module functions
$Path = Join-Path $PSScriptRoot 'Public'
$Functions = Get-ChildItem -Path $Path -Filter '*.ps1' #1
Foreach ($import in $Functions) { #2
Try {
Write-Verbose "dot-sourcing file '$($import.fullname)'"
. $import.fullname #3
}
Catch {
Write-Error -Message "Failed to import function $($import.name)"
}
}
This loading pattern finds .ps1 files beneath Public and dot-sources each one, with verbose logging and error reporting for failed imports. section 2.2.2
