liveBook · Manning

Psm1 file

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

A PSM1 file is a PowerShell module script file that implements or loads a module's functions and supporting code.

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

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Defines PSM1 modules and demonstrates loading split function files, while deferring in-depth module creation.
Examples
Demonstrates FileCleanupTools, Public and Private function folders, and log-file archival automation.
What it assumes
Assumes PowerShell functions and scripts, then introduces basic script-module file layout and loading.
What you can do after
Build it from scratch as a reusable script module that imports selected functions into an automation script.
How it connects
Connects script modules to module manifests, Import-Module, dot-sourcing, public functions, and private functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.