PowerShell Modules
A module is a package of PowerShell commands: cmdlets, functions, variables and aliases that you load as a unit. Every cmdlet you've used lives in a module (Get-ChildItem is in Microsoft.PowerShell.Management). Modules are also how you share your own functions between scripts and with your team.
Finding modules
(Get-Command Get-ChildItem).ModuleName
(Get-Command ConvertTo-Json).ModuleName
(Get-Command Get-FileHash).ModuleName
Get-Module -ListAvailable -Name Microsoft.PowerShell.* |
Sort-Object Name -Unique | ForEach-Object NameMicrosoft.PowerShell.Management
Microsoft.PowerShell.Utility
Microsoft.PowerShell.Utility
Microsoft.PowerShell.Archive
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.PowerShell.ThreadJob
Microsoft.PowerShell.Utility
Get-Module alone lists the modules loaded in the session; -ListAvailable lists everything installed in the folders of $env:PSModulePath. You rarely need Import-Module for installed modules: PowerShell auto-loads a module the first time you use one of its commands.
Installing from the PowerShell Gallery
The PowerShell Gallery hosts thousands of community and vendor modules, such as Az (Azure), AWS.Tools, Pester (testing) and PSScriptAnalyzer (linting). PowerShell 7.4+ uses the PSResourceGet cmdlets; the older PowerShellGet commands still work:
Find-PSResource -Name Pester
Install-PSResource -Name Pester -Scope CurrentUser
Update-PSResource -Name Pester
Get-InstalledPSResource
# Older equivalents
Find-Module Pester
Install-Module Pester -Scope CurrentUser-Scope CurrentUser installs into your home folder and needs no administrator rights. Always check who publishes a module before installing it.
Writing a script module
A script module is just a .psm1 file containing functions. Import it with Import-Module and its functions become commands:
New-Item -ItemType Directory ./TextTools -Force | Out-Null
Set-Content ./TextTools/TextTools.psm1 @'
function Get-WordCount {
param([Parameter(ValueFromPipeline)][string]$Text)
process { @($Text -split '\s+' | Where-Object { $_ }).Count }
}
function ConvertTo-TitleCase {
param([Parameter(ValueFromPipeline)][string]$Text)
process { (Get-Culture).TextInfo.ToTitleCase($Text.ToLower()) }
}
function Get-Internal { 'helper not meant for users' }
Export-ModuleMember -Function Get-WordCount, ConvertTo-TitleCase
'@
Import-Module ./TextTools/TextTools.psm1
'the quick brown fox' | Get-WordCount
'the QUICK brown fox' | ConvertTo-TitleCase
(Get-Command -Module TextTools).Name -join ', '
[bool](Get-Command Get-Internal -ErrorAction SilentlyContinue)4
The Quick Brown Fox
ConvertTo-TitleCase, Get-WordCount
False
Export-ModuleMember decides which functions are public. Everything else stays private to the module, so you can keep helpers without cluttering the user's session. Without Export-ModuleMember, all functions are exported.
Module manifests
A manifest (.psd1) describes a module: version, author, dependencies and what it exports. New-ModuleManifest creates one:
# Each example runs on its own, so first recreate a tiny module
New-Item -ItemType Directory ./TextTools -Force | Out-Null
Set-Content ./TextTools/TextTools.psm1 'function Get-WordCount { param($Text) @($Text -split " ").Count }'
New-ModuleManifest -Path ./TextTools/TextTools.psd1 `
-RootModule 'TextTools.psm1' `
-ModuleVersion '1.0.0' `
-Author 'Ada Lovelace' `
-Description 'Small text helpers' `
-FunctionsToExport 'Get-WordCount'
$info = Test-ModuleManifest ./TextTools/TextTools.psd1
"$($info.Name) $($info.Version) by $($info.Author)"
$info.ExportedFunctions.Keys -join ', 'TextTools 1.0.0 by Ada Lovelace
Get-WordCount
When the module folder (named like the module) sits in one of the $env:PSModulePath folders, Import-Module TextTools finds it by name, and auto-loading works.
Module layout
TextTools/
├── TextTools.psd1 # manifest: version, exports, dependencies
├── TextTools.psm1 # root module: dot-sources the files below
├── Public/
│ ├── Get-WordCount.ps1
│ └── ConvertTo-TitleCase.ps1
├── Private/
│ └── Get-Internal.ps1
└── Tests/
└── TextTools.Tests.ps1 # Pester testsReloading and removing
New-Item -ItemType Directory ./TextTools -Force | Out-Null
Set-Content ./TextTools/TextTools.psm1 'function Get-WordCount { param($Text) @($Text -split " ").Count }'
New-ModuleManifest ./TextTools/TextTools.psd1 -RootModule TextTools.psm1 -ModuleVersion 1.0.0
Import-Module ./TextTools/TextTools.psd1 -Force # -Force reloads after edits
(Get-Module TextTools).Version.ToString()
Remove-Module TextTools
[bool](Get-Module TextTools)1.0.0
False
Tip: During development, run
Import-Module ./MyModule -Forceafter each change. Without-Force, PowerShell keeps the already-loaded old version.
Test yourself
What file extension does a script module use?
- .ps1
- .psm1
- .psd1
- .dll
What does Export-ModuleMember -Function Get-A do in a .psm1?
- Deletes Get-A
- Makes only Get-A visible to users of the module
- Exports Get-A to a file
- Publishes Get-A to the Gallery
Why don't you usually need Import-Module for installed modules?
- They are always loaded at startup
- Module auto-loading imports a module the first time one of its commands is used
- Import-Module is deprecated
- They are compiled into PowerShell