PowerShell Modules
A module is a package of PowerShell commands — cmdlets, functions, variables, and aliases — bundled together for a specific purpose. Instead of writing the same functions in every script, they are placed in a module once, and any script can import and use them. Modules are how PowerShell manages Azure, Active Directory, Exchange, SQL Server, networking, and hundreds of other technologies.
What Is a Module?
+-------------------------------+ | PowerShell Module | |-------------------------------| | Functions (reusable logic) | | Cmdlets (built-in tools) | | Variables (shared data) | | Aliases (shortcuts) | | Help files (documentation) | +-------------------------------+ Saved as .psm1 (script module) or compiled as .dll (binary module)
Types of Modules
| Module Type | File Extension | Description |
|---|---|---|
| Script Module | .psm1 | PowerShell functions saved in a file |
| Binary Module | .dll | Compiled .NET code (used by Azure, AD modules) |
| Manifest Module | .psd1 | Metadata file describing a module |
| Dynamic Module | (in memory) | Created with New-Module at runtime |
Finding Modules
# List all installed modules
Get-Module -ListAvailable
# List currently imported modules in this session
Get-Module
# Search for a module on the PowerShell Gallery (requires internet)
Find-Module -Name "Az"
Find-Module -Name "*SQL*"
# Search by keyword
Find-Module -Tag "Azure"
Installing Modules from PowerShell Gallery
The PowerShell Gallery (https://www.powershellgallery.com) is Microsoft's public repository for modules — similar to npm for Node.js or pip for Python.
# Install a module from PowerShell Gallery
Install-Module -Name Az # Azure module
Install-Module -Name ActiveDirectory # AD management
Install-Module -Name ImportExcel # Excel without Office
# Install for current user only (no Admin needed)
Install-Module -Name ImportExcel -Scope CurrentUser
# Install a specific version
Install-Module -Name Az -RequiredVersion "10.0.0"
# Trust the repository first (skip confirmation prompt)
Install-Module -Name Az -Force -AllowClobber
Importing Modules
PowerShell 3 and later auto-imports installed modules when a command from that module is used. Manual import is still useful to confirm availability or force loading.
# Import a module manually
Import-Module -Name ActiveDirectory
Import-Module -Name Az.Accounts
# Import and see what was imported
Import-Module -Name ImportExcel -PassThru
# Import from a specific path
Import-Module -Name "C:\MyModules\UtilityTools.psm1"
Removing Modules
# Remove from current session (does not uninstall)
Remove-Module -Name ActiveDirectory
# Uninstall module completely
Uninstall-Module -Name ImportExcel
Creating a Custom Script Module
A script module is a .psm1 file containing reusable functions. It lets shared code live in one place rather than being copied into every script.
Step 1 – Create the Module File
Save this as C:\MyModules\MyTools\MyTools.psm1:
# MyTools.psm1 – Custom utility module
function Get-DiskSummary {
Get-PSDrive -PSProvider FileSystem | Select-Object Name,
@{Name="UsedGB"; Expression={[math]::Round($_.Used/1GB,2)}},
@{Name="FreeGB"; Expression={[math]::Round($_.Free/1GB,2)}}
}
function Write-Log {
param (
[string]$Message,
[string]$Level = "INFO",
[string]$LogFile = "C:\Logs\app.log"
)
$entry = "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] [$Level] $Message"
Add-Content -Path $LogFile -Value $entry
Write-Host $entry
}
function Get-FileAge {
param ([string]$FilePath)
$file = Get-Item $FilePath
return ((Get-Date) - $file.LastWriteTime).Days
}
# Export only these functions (hide internal helpers)
Export-ModuleMember -Function Get-DiskSummary, Write-Log, Get-FileAge
Step 2 – Import and Use the Module
Import-Module -Name "C:\MyModules\MyTools\MyTools.psm1"
Get-DiskSummary
Write-Log -Message "Backup started" -Level "INFO"
$age = Get-FileAge -FilePath "C:\Data\config.xml"
Write-Host "File is $age days old"
Module Manifest File (.psd1)
A module manifest describes the module — its version, author, dependencies, and which functions to export. It is the professional way to package a module.
# Create a manifest file automatically
New-ModuleManifest -Path "C:\MyModules\MyTools\MyTools.psd1" `
-RootModule "MyTools.psm1" `
-ModuleVersion "1.0.0" `
-Author "eStudy247" `
-Description "Utility functions for system administration" `
-FunctionsToExport @("Get-DiskSummary", "Write-Log", "Get-FileAge")
Generated .psd1 (partial):
ModuleVersion = '1.0.0'
GUID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
Author = 'eStudy247'
Description = 'Utility functions for system administration'
RootModule = 'MyTools.psm1'
FunctionsToExport = @('Get-DiskSummary','Write-Log','Get-FileAge')
Module Auto-Loading – PSModulePath
PowerShell automatically searches specific folders for modules. Placing a module in one of these folders makes it available without specifying the full path.
# View the module search paths
$env:PSModulePath -split ";"
Common paths:
C:\Users\Admin\Documents\PowerShell\Modules– Current user modulesC:\Program Files\PowerShell\7\Modules– System-wide PowerShell 7 modulesC:\Windows\System32\WindowsPowerShell\v1.0\Modules– Built-in Windows modules
To auto-load the custom module, place the MyTools folder inside one of these paths:
# Copy module to user module path
Copy-Item -Path "C:\MyModules\MyTools" `
-Destination "$env:USERPROFILE\Documents\PowerShell\Modules\MyTools" `
-Recurse
# Now it loads automatically
Import-Module MyTools # No full path needed
Updating and Managing Installed Modules
# Check for available updates
Get-InstalledModule | ForEach-Object {
$latest = Find-Module -Name $_.Name -ErrorAction SilentlyContinue
if ($latest -and [version]$latest.Version -gt [version]$_.Version) {
Write-Host "Update available: $($_.Name) $($_.Version) → $($latest.Version)"
}
}
# Update a specific module
Update-Module -Name Az
# Update all installed modules
Get-InstalledModule | Update-Module
Popular PowerShell Modules
| Module | Purpose | Install Command |
|---|---|---|
| Az | Microsoft Azure management | Install-Module Az |
| ActiveDirectory | Manage AD users, groups, OUs | Included in RSAT tools |
| ImportExcel | Read/write Excel without Office | Install-Module ImportExcel |
| Pester | PowerShell unit testing framework | Install-Module Pester |
| PSWindowsUpdate | Manage Windows Updates | Install-Module PSWindowsUpdate |
| SqlServer | SQL Server management | Install-Module SqlServer |
| AWS.Tools | AWS cloud management | Install-Module AWS.Tools.Common |
| PSScriptAnalyzer | Code quality linting | Install-Module PSScriptAnalyzer |
Summary
Modules are the packaging system for PowerShell code. The PowerShell Gallery provides thousands of ready-to-use modules. Custom modules bundle shared functions into reusable .psm1 files. Module manifests (.psd1) add versioning, authorship, and dependency management. Placing modules in the PSModulePath enables auto-loading. Export-ModuleMember controls which functions are public. Understanding modules is the key to building maintainable, shareable PowerShell solutions that scale from a single script to an enterprise automation library.
