05 / REUSABLE AUTOMATION · LESSON 19

Organise tools in a module

Import a local script module and call its exported function.

12 min · learn at your pace

Give related functions a home

A simple script module is a .psm1 file. It can contain functions, variables, and explicit exports. A module keeps reusable implementation separate from the script that calls it.

The lab includes Tools.psm1:

function Get-LabGreeting {
    param([string]$Name)
    "Welcome, $Name"
}
Export-ModuleMember -Function Get-LabGreeting

Import and inspect

Import-Module './Tools.psm1'
Get-Command -Module Tools
Get-LabGreeting -Name 'Ada'

Export-ModuleMember chooses which functions a caller can use. Get-Module shows loaded modules. When developing locally, Import-Module -Force reloads an edited script module.

Larger modules can include a .psd1 manifest with metadata and requirements. You do not need a manifest for this small local example.

This lesson uses a supplied local module. Installing arbitrary modules from an online gallery is outside the training lab, and some modules depend on native operating-system features unavailable in a browser.

Try an example.ps1
Get-Content './Tools.psm1'
YOUR TURN

Put it into practice.

Import ./Tools.psm1 and call Get-LabGreeting with Name Ada. Output the function's greeting.

See an explained solution
One way to solve it.ps1
Import-Module './Tools.psm1' -Force
Get-LabGreeting -Name 'Ada'

Import-Module loads the module into the session. Its exported function becomes available to your script.