2 min read

PowerShell Modules

Modules are packages that contain PowerShell members, such as cmdlets, providers, functions, variables, and aliases. They allow you to organize your code and share it with others.

Parent Topic: PowerShell Scripting and Programming Related: PowerShell Comprehensive Guide

1. Using Modules

Finding and Installing

The PowerShell Gallery is the central repository for PowerShell content.

# Find a module
Find-Module -Name "PSReadLine"

# Install a module (scope CurrentUser is recommended for non-admins)
Install-Module -Name "PSReadLine" -Scope CurrentUser

Importing and Listing

PowerShell usually imports installed modules automatically when you use a command from them. However, you can manage them manually.

# List loaded modules
Get-Module

# List all available modules on disk
Get-Module -ListAvailable

# Manually import a module
Import-Module -Name "ActiveDirectory"

2. Creating a Module

A module is essentially a PowerShell script saved with a .psm1 extension.

Step 1: Create the Script Module (.psm1)

Create a file named MyTools.psm1.

# MyTools.psm1

function Get-Hello {
    param($Name)
    return "Hello, $Name"
}

function Get-Goodbye {
    param($Name)
    return "Goodbye, $Name"
}

# Export specific functions to be visible to the user
Export-ModuleMember -Function Get-Hello, Get-Goodbye

Step 2: Import Your Module

Import-Module .\MyTools.psm1
Get-Hello -Name "World"

3. Module Manifests (.psd1)

A manifest is a hash table that describes the module (version, author, dependencies). It is required for publishing to the Gallery.

# Generate a manifest file
New-ModuleManifest -Path .\MyTools.psd1 -RootModule .\MyTools.psm1 -Author "Your Name" -Description "My custom tools"

Return to PowerShell Scripting and Programming