liveBook · Manning

Script module

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

A script module is a PowerShell module implemented with PowerShell code that packages functions, classes, variables, and supporting resources.

A script module packages related PowerShell functions, classes, and variables so they can be shared across automations instead of recreated inside individual scripts. It turns general-purpose automation building blocks into a reusable unit that teammates can extend together. Script modules can also supplement base or gallery modules without modifying compiled cmdlets or upgrade-sensitive code. section 2.2

A module without compiled code is a script module. At its simplest, it is a PowerShell file saved with the .psm1 extension, but a maintainable module also uses a .psd1 manifest. The manifest records module details, controls which functions or variables are public, and carries a version number. section 2.2.2

Organize a reusable script module around a stable loading and versioning structure:

  • Give the top-level folder, .psm1 file, and .psd1 manifest the same module name so standard Import-Module loading can locate it. section 2.2.2
  • Place releases in folders named for their version, allowing multiple versions of the same module to exist on one computer. section 2.2.2
  • Keep the .psm1 as a loader and place functions in separate .ps1 files rather than one oversized module file. section 2.2.2
  • Use Public and Private folders when functions need different visibility; load the relevant files from the module loader and handle loading failures. section 2.2.2

Turning log cleanup into FileCleanupTools

A log-cleanup automation can import FileCleanupTools, calculate an archive date, find older files, build an archive path with Set-ArchiveFilePath, compress the files, and remove the archived originals with Remove-ArchivedFiles. Moving those reusable tasks out of the operational script leaves the script focused on its inputs and workflow. section 2.2.2

Encapsulating a process report

Get-TopProcess accepts a required TopN parameter, retrieves processes, sorts them by CPU use, selects the requested count, and formats ID, process name, and CPU output. A function like this is a candidate for a module when several automations need the same report. section 2.2

Figure 2.5 A PowerShell module folder structure with version folders

This folder diagram helps verify the name-and-version layout used to make a script module loadable and to retain multiple releases. section 2.2.2

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

How deep it goes
Defines script modules and outlines .psm1, .psd1, version folders, and function-loading patterns, while deferring in-depth module creation.
Examples
Uses a top-CPU process report and a FileCleanupTools log-archive workflow with Set-ArchiveFilePath and Remove-ArchivedFiles.
What it assumes
Assumes familiarity with PowerShell functions and scripts, then teaches basic script-module file and folder setup.
What you can do after
Build it from scratch as a versioned PowerShell module that loads reusable functions into automation scripts.
How it connects
Connects functions, classes, variables, module manifests, version control, unit testing, and Import-Module.

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.