liveBook · Manning

Module

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

A PowerShell module is a package that groups commands, functions, variables, and related resources for reuse.

A PowerShell module packages reusable commands, functions, variables, and supporting resources. A script module uses a .psm1 file, turning focused functions into commands that PowerShell can discover and load on demand. section 10.2 section 10.6

Build a practical script module by following these steps:

  • Keep functions tightly scoped and self-contained; accept declared parameters and write useful objects to the pipeline. section 10.6
  • Create a module folder and a matching .psm1 filename beneath a directory listed in $env:PSModulePath. section 10.2 section 16.1
  • Import the module when testing, then unload or reload it after edits because PowerShell runs the loaded version from memory. section 10.4
  • Add a .psd1 manifest as complexity grows; specify the root module, prerequisites, version, metadata, and explicitly exported members. section 16.1 section 16.2
  • Export only public functions, variables, and aliases, leaving helper functions private; avoid wildcard exports when load performance matters. section 16.3.5

A module that grows with the tool

A MolTools script module can expose Set-TMServiceLogon with service, password, username, and computer-name parameters. Test it against BITS on the local host or several servers, then unload and reload the module after changes. A larger module can keep a database connection string and a table-creation function private while exporting Get-DiskInfo. section 10.5.2 section 26.3

Get-MachineInfo -ComputerName localhost

Use the Get-MachineInfo command to verify that PowerShell discovers the module and exposes its function as a command. section 10.4

Modules can remain local reusable tools or become shared packages. Before publishing, search the PowerShell Gallery for competing functionality; when revising a published module, update its manifest version so users can retrieve it with Update-Module. section 22.4.1 section 22.5.1

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Descends from basic .psm1 creation through loading, reloading, manifests, exports, private helpers, discovery performance, and revision management.
Examples
Uses Get-MachineInfo, Set-TMServiceLogon, Get-DiskInfo, MyTools.psm1, database code, and PowerShell Gallery searches.
What it assumes
Assumes PowerShell scripting, functions, parameters, pipeline output, and CIM usage while teaching module paths, manifests, exports, and reloading.
What you can do after
Build it from scratch as a discoverable script module, test its commands, control its public surface, and publish versioned revisions.
How it connects
Connects functions, pipelines, PSModulePath, module manifests, PowerShell Gallery, Export-ModuleMember, and Update-Module.

Also explained in

Compare how 35 books explain module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.