Skip to content
AgileLogics
6 min read

Modules

Prerequisites: Advanced functions

A module is a package of commands. PowerShell ships with several; thousands more are on the PowerShell Gallery.

Finding and installing modules

# illustrative
Get-Module -ListAvailable              # installed modules
Find-Module -Name *Excel*              # search the Gallery
Install-Module -Name ImportExcel -Scope CurrentUser
Get-Command -Module ImportExcel        # what it provides
Update-Module -Name ImportExcel
  • -Scope CurrentUser installs without administrator rights.
  • Modules auto-load the first time you use one of their commands — you rarely need Import-Module.
  • PowerShell 7.4+ also includes Install-PSResource, the newer replacement for Install-Module.

Popular modules: Az (Azure), Microsoft.Graph (Microsoft 365), ImportExcel, Pester (testing), PSScriptAnalyzer (linting), dbatools (SQL Server).

Writing your own module

A script module is simply a .psm1 file with functions:

New-Item -ItemType Directory -Path MathTools | Out-Null
Set-Content MathTools/MathTools.psm1 @'
function Get-Square {
  param([double]$Number)
  $Number * $Number
}

function Get-Cube {
  param([double]$Number)
  $Number * (Get-Square $Number)
}

function Format-Helper { 'private' }

Export-ModuleMember -Function Get-Square, Get-Cube
'@

Import-Module ./MathTools
Get-Cube 3
Get-Command -Module MathTools | Select-Object -ExpandProperty Name
27
Get-Cube
Get-Square
  • The folder and .psm1 file share the module's name.
  • Export-ModuleMember makes only the listed functions public; Format-Helper stays private.
  • Put the folder in a path listed in $env:PSModulePath (for example Documents\PowerShell\Modules) and it auto-loads like any other module.

Module manifests

A manifest (.psd1) adds a version, author, description, required modules, and the list of exported functions:

# illustrative
New-ModuleManifest -Path MathTools/MathTools.psd1 `
  -RootModule MathTools.psm1 -ModuleVersion 1.0.0 `
  -Author 'Ada' -FunctionsToExport 'Get-Square', 'Get-Cube'

A manifest is required to publish a module, and listing FunctionsToExport explicitly makes auto-loading faster. Level 3 covers structuring and publishing larger modules.