Code Reference

Modules

PowerShell · Reference cheat sheet

Modules

PowerShell · Reference cheat sheet


📋 Overview

Modules package functions, cmdlets, and resources for reuse. Import with Import-Module; discover with Get-Module -ListAvailable. Prefer versioned modules from the PowerShell Gallery and explicit manifests (.psd1) for production.

🔧 Core concepts

PieceRole
.psm1Script module
.psd1Manifest (exports, deps, version)
Import-ModuleLoad
Remove-ModuleUnload
Get-ModuleLoaded / available
Install-ModuleFrom Gallery (PSGet)
$env:PSModulePathSearch paths
AutoloadingCommands trigger import

Export explicitly via Export-ModuleMember or manifest FunctionsToExport—avoid accidental exports.

💡 Examples

Import and find:

Get-Module -ListAvailable | Select-Object Name, Version
Import-Module ThreadJob -Force
Get-Command -Module ThreadJob

Install from Gallery:

Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module -Name PSScriptAnalyzer -Scope CurrentUser

Minimal module:

# MyUtils.psm1
function Get-Hello { 'hello' }
Export-ModuleMember -Function Get-Hello
# MyUtils.psd1 (sketch)
@{
  RootModule = 'MyUtils.psm1'
  ModuleVersion = '1.0.0'
  FunctionsToExport = @('Get-Hello')
}

⚠️ Pitfalls

  • Multiple versions on PSModulePath—pin with -RequiredVersion when needed.
  • CurrentUser vs AllUsers install scopes (admin rights).
  • Binary modules differ by edition (Windows PowerShell 5.1 vs PowerShell 7+).
  • Dot-sourcing .ps1 is not a module—state and exports behave differently.
  • Gallery modules: review publishers; use Install-Module -WhatIf / checksums where required.
  • Circular imports and side effects at import time make debugging hard—keep imports thin.

On this page