Copy-pasting the same three helper functions into every runbook is how drift starts. One copy gets a bug fix, four do not, and eighteen months later nobody can tell you which version of Get-DiskReport is the real one.
The fix is not discipline. It is packaging. PowerShell has a proper module system that lets your team run Import-Module OpsToolkit instead of pasting code, and it has been sitting there, unused, in most estates I review. This is how to structure a real module: a .psm1 that dot-sources your functions, a .psd1 manifest that controls what is exported and what version people installed, and a Pester 5 suite you run in CI so the module cannot regress silently. Everything here targets PowerShell 7.4, which is the current long-term-support release and cross-platform.
The layout that actually installs
A module is a directory whose name matches the module. Inside it live the manifest (.psd1), the root script module (.psm1), and your function files. The pattern I reach for keeps one exported function per file in a Public folder, so the module is easy to read and the manifest export list writes itself.
Free · 4 minutes
Do you actually know what you are running — and what it is about to cost you?
Fourteen questions on the systems you depend on, the ones nobody owns, and the support dates that turn a routine upgrade into a forced re-platform. Banded finding on screen, full sheet by email.
OpsToolkit/
├── OpsToolkit.psd1 # the manifest
├── OpsToolkit.psm1 # the root module (loader)
├── Public/
│ └── ConvertTo-SafeName.ps1
└── tests/
└── ConvertTo-SafeName.Tests.ps1
The .psm1 does not contain the functions. It finds them and loads them. Dot-sourcing each file into the module scope keeps the source tidy and means adding a function is a matter of dropping a file into Public.
# OpsToolkit.psm1 — the loader
$public = Get-ChildItem -Path "$PSScriptRoot/Public" -Filter '*.ps1' -ErrorAction SilentlyContinue
foreach ($file in $public) {
try {
. $file.FullName
}
catch {
Write-Error "Failed to import $($file.FullName): $_"
}
}
Export-ModuleMember -Function $public.BaseName
Here is the example function the rest of the post tests. It is deliberately pure — no network, no filesystem — so the tests are deterministic. In a real toolkit this folder holds your disk reports, endpoint checks and log parsers.
# Public/ConvertTo-SafeName.ps1
function ConvertTo-SafeName {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[string] $Name
)
process {
# Collapse runs of non-alphanumerics to a single hyphen, trim, lower-case
($Name -replace '[^a-zA-Z0-9]+', '-').Trim('-').ToLower()
}
}
The manifest is the contract
The .psd1 manifest is the thing that turns a folder of scripts into a versioned, publishable module. It is the contract between the module and everyone who imports it — the same discipline as designing an API contract before the code. Generate it with New-ModuleManifest rather than hand-writing it, because the cmdlet produces a valid, fully-commented file every time.
New-ModuleManifest -Path './OpsToolkit/OpsToolkit.psd1' `
-RootModule 'OpsToolkit.psm1' `
-ModuleVersion '1.0.0' `
-Author 'Platform Engineering' `
-Description 'Shared operational helpers for the estate' `
-PowerShellVersion '7.4' `
-FunctionsToExport @('ConvertTo-SafeName') `
-CmdletsToExport @() `
-VariablesToExport @() `
-AliasesToExport @()
Two fields carry most of the weight. ModuleVersion is what Get-Module reports and what a package feed keys on; bump it on every release and your team can actually answer “which version is running”. FunctionsToExport is the public surface. List the functions explicitly. The default that New-ModuleManifest writes if you say nothing is '*', which exports everything and forces PowerShell to load the whole module just to discover its commands. An explicit array is both faster and honest about what is API and what is internal. The same logic applies to CmdletsToExport, AliasesToExport and VariablesToExport — set them to empty arrays rather than leaving the wildcards in place.
Treating exported members as a deliberate, declared list is the same instinct behind keeping a bill of materials for what you ship: if you cannot say precisely what is in the package, you cannot govern it.
Pester 5 is not Pester 4
If you last wrote PowerShell tests a few years ago, the muscle memory is wrong. Pester 5 runs in two phases: discovery, where it executes the file top-to-bottom to find every Describe, Context and It, and run, where it executes the code inside them. The practical consequence is that setup — importing your module, building fixtures — must live inside a BeforeAll block, not bare at the top of the file. Bare setup code runs during discovery, before the test framework is ready, and your module will not be imported when the assertions run.
# tests/ConvertTo-SafeName.Tests.ps1
BeforeAll {
# Import the module under test from the manifest, forcing a fresh load
Import-Module "$PSScriptRoot/../OpsToolkit.psd1" -Force
}
Describe 'ConvertTo-SafeName' {
It 'lower-cases and hyphenates a name' {
ConvertTo-SafeName -Name 'Prod DB 01' | Should -Be 'prod-db-01'
}
It 'strips leading and trailing separators' {
ConvertTo-SafeName -Name ' Weekly Report! ' | Should -Be 'weekly-report'
}
It 'accepts pipeline input' {
'A B', 'C D' | ConvertTo-SafeName | Should -Be @('a-b', 'c-d')
}
}
Import from the manifest (OpsToolkit.psd1), not the .psm1. Importing the manifest exercises the same path your users take, so the test catches a broken export list or a bad version constraint — exactly the mistakes that never show up when you dot-source the function file directly. The -Force flag guarantees you are testing the code on disk, not a stale copy already loaded in the session.
Wire it into CI
Pester 5 is driven from a configuration object, which is how you get machine-readable output and a non-zero exit code when something fails — the two things a pipeline needs. Run PSScriptAnalyzer in the same step so style and correctness lint failures block the merge alongside the tests.
# build.ps1 — the CI entry point
#Requires -Version 7.4
$ErrorActionPreference = 'Stop'
# Static analysis first — cheap, catches the obvious
$issues = Invoke-ScriptAnalyzer -Path ./OpsToolkit -Recurse -Severity @('Warning', 'Error')
if ($issues) {
$issues | Format-Table -AutoSize
throw "PSScriptAnalyzer found $($issues.Count) issue(s)."
}
$config = New-PesterConfiguration
$config.Run.Path = './OpsToolkit/tests'
$config.Run.Exit = $true # exit non-zero on failure
$config.CodeCoverage.Enabled = $true
$config.CodeCoverage.Path = './OpsToolkit/Public'
$config.TestResult.Enabled = $true
$config.TestResult.OutputPath = './testResults.xml'
$config.Output.Verbosity = 'Detailed'
Invoke-Pester -Configuration $config
Setting Run.Exit to $true is the line that matters for CI: without it, a failing test still returns exit code 0 and your pipeline goes green over a broken build. TestResult.OutputPath emits NUnit XML that GitHub Actions, Azure Pipelines and Jenkins all parse into a test report. On a fresh runner, install both tools first with Install-Module Pester -MinimumVersion 5.0 -Force and Install-Module PSScriptAnalyzer -Force, because the Pester 3 build shipped in the Windows PowerShell box is not the one you want.
Managing your own tooling this cleanly is a habit worth spreading across languages — it is the same argument for installing Python CLI tools in isolated environments rather than polluting the system interpreter.
Once this is in place, the module has a version, a declared public surface, and a test suite that fails the build before a regression reaches anyone’s runbook. The copied-function problem does not come back, because there is nothing left to copy — there is a module to import. That is the whole point: the drift you were managing by memory is now managed by the manifest.
Free interactive tool
Website compliance checklist
What your site has to do, based on what it actually does
Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.
Everything that applies
Ordered by what to do first: legal requirements you can close quickly, then larger pieces of work, then what is expected rather than required. Not exhaustive, and not a legal audit.
Dated PDF, yours to keep or circulate.
Most technology problems are not technology problems. They are control problems.
The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.