- Courses
- 💻Programming Languages
- 🪟PowerShell
- PowerShell Scripting at Scale
- Modules & Packaging
- Creating Modules
On this page
Creating PowerShell Modules: Step-by-Step Guide
Updated on June 23, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
A PowerShell module is the professional way to package reusable code. A script module begins as a valid PowerShell script saved with a .psm1 extension. The module's folder name must match the script name, and the folder should be placed in a path specified in $Env:PSModulePath. By default, all functions in the .psm1 file are accessible when imported, but professional practice dictates explicitly exporting only public functions using Export-ModuleMember. The recommended structure separates public and private functions into distinct folders, with the .psm1 file dot-sourcing all functions and exporting only the public ones. A module manifest (.psd1) adds metadata, versioning, and dependency management. Placing the module in a standard module path makes it available system-wide. Creating modules transforms PowerShell from a scripting tool into a scalable automation framework.
The Core Concept: A Module Is Just a Folder
A PowerShell module is fundamentally:
- A folder
- With a name that matches the module
- Containing a
.psm1file (script module) - And optionally a
.psd1file (manifest)
Example:
MyModule
│
└── MyModule.psm1
At its most basic, PowerShell treats any valid PowerShell script saved in a .psm1 file as a module. If PowerShell sees this folder in a module path, it recognizes it as a module.
Step 1 — Create the Module Folder
Choose a name for your module. This name becomes:
- The folder name
- The module name
- The name used in
Import-Module
Example:
New-Item -ItemType Directory -Path "C:\Modules\MyModule"
Code explanation: New-Item -ItemType Directory creates a new folder. -Path specifies the full path to the new folder. The folder name MyModule becomes the module name.
Your folder structure now looks like:
C:\Modules\MyModule
Step 2 — Create the .psm1 File
Inside the folder, create a .psm1 file with the same name:
New-Item -ItemType File -Path "C:\Modules\MyModule\MyModule.psm1"
Code explanation: New-Item -ItemType File creates a new empty file. The .psm1 extension signals to PowerShell that this is a script module. The file name must match the folder name.
Step 3 — Add Functions to the Module
Open the .psm1 file and add one or more functions.
Example:
function Get-Greeting {
param([string]$Name)
"Hello, $Name"
}
function Get-Timestamp {
Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
Code explanation: Functions are defined using the function keyword. param([string]$Name) declares a parameter with a string type. The functions return string output when called. These functions are now part of the module.
Step 4 — Export the Public Functions
By default, PowerShell exports all functions in a .psm1 file. Professional modules explicitly export only the public API.
Add this to the bottom of the .psm1 file:
Export-ModuleMember -Function Get-Greeting, Get-Timestamp
Code explanation: Export-ModuleMember specifies which functions are exposed to users. Only the listed functions become available when the module is imported. Helper functions not listed remain private, creating a clean, predictable interface.
Step 5 — Create a Module Manifest (Recommended)
A manifest (.psd1) adds metadata such as:
- Version
- Author
- Dependencies
- Exported functions
- Required PowerShell version
Create it with:
New-ModuleManifest -Path "C:\Modules\MyModule\MyModule.psd1" `
-RootModule "MyModule.psm1" `
-Author "Your Name" `
-ModuleVersion "1.0.0"
Code explanation: New-ModuleManifest generates a structured metadata file. -RootModule points to the primary script module file. -Author and -ModuleVersion are required fields for professional modules. The manifest is required to publish a module to the PowerShell Gallery.
Why a Manifest Matters
- Enables versioning
- Required for publishing to repositories
- Controls exports through
FunctionsToExport - Supports dependency management with
RequiredModules - Documents the module's purpose and author
Step 6 — Import and Test the Module
Import the module:
Import-Module "C:\Modules\MyModule"
Code explanation: Import-Module loads the module into the current session. The path can point to the folder containing the .psm1 file. When the module is in a standard module path, you can import it by name only.
Test the functions:
Get-Greeting -Name "Enrico"
Get-Timestamp
Code explanation: After import, the exported functions are available in the session. Running Get-Greeting -Name "Enrico" should output Hello, Enrico. Running Get-Timestamp should output the current date and time. If the functions run, the module is working.
Step 7 — Add a Public/Private Folder Structure (Professional Practice)
As modules grow, separate public and private functions:
MyModule
│
├── MyModule.psm1
├── MyModule.psd1
├── Public
│ ├── Get-Greeting.ps1
│ └── Get-Timestamp.ps1
└── Private
└── Convert-Name.ps1
Code explanation: The Public folder contains functions intended for users. The Private folder contains internal helper functions not exposed to users. Each function resides in its own .ps1 file, improving maintainability.
Then, in your .psm1 file:
# Load public functions
Get-ChildItem "$PSScriptRoot\Public\*.ps1" | ForEach-Object { . $_ }
# Load private functions
Get-ChildItem "$PSScriptRoot\Private\*.ps1" | ForEach-Object { . $_ }
# Export only public functions
Export-ModuleMember -Function (Get-ChildItem "$PSScriptRoot\Public\*.ps1").BaseName
Code explanation: $PSScriptRoot is an automatic variable containing the directory of the .psm1 file. Get-ChildItem *.ps1 | ForEach-Object { . $_ } dot-sources each script file, loading its functions into the module. (Get-ChildItem ...).BaseName extracts the file names (without extension) to export only the public functions. This structure keeps the module clean, makes maintenance easier, and supports large codebases.
Step 8 — Place the Module in a Module Path
To make the module available system-wide, place it in a module path:
- Current user:
$HOME\Documents\PowerShell\Modules - All users:
$Env:ProgramFiles\PowerShell\Modules
Example:
Copy-Item -Path "C:\Modules\MyModule" -Destination "$HOME\Documents\PowerShell\Modules" -Recurse
Code explanation: Copy-Item -Recurse copies the entire module folder. When placed in a directory listed in $Env:PSModulePath, PowerShell discovers the module automatically. Modules in the all-users path are available to everyone; those in the current-user path are available only to that user.
Now you can import it from anywhere:
Import-Module MyModule
Code explanation: When the module is in a standard module path, you can import it by name only, without specifying the full path. PowerShell locates the module automatically.
Summary
Creating a PowerShell module involves:
- Creating a folder named after the module
- Adding a
.psm1file with your functions - Exporting the public functions with
Export-ModuleMember - Creating a manifest for metadata and versioning
- Organizing code into
PublicandPrivatefolders - Placing the module in a module path
- Importing and testing it
Modules are the foundation of professional PowerShell development. They enable reuse, maintainability, versioning, and distribution—everything required for enterprise-grade automation.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Modules
Next
Publishing Modules
-
The Core Concept: A Module Is Just a Folder
-
Step 1 — Create the Module Folder
-
Step 2 — Create the
.psm1File -
Step 3 — Add Functions to the Module
-
Step 4 — Export the Public Functions
-
Step 5 — Create a Module Manifest (Recommended)
- Why a Manifest Matters
-
Step 6 — Import and Test the Module
-
Step 7 — Add a Public/Private Folder Structure (Professional Practice)
-
Step 8 — Place the Module in a Module Path
-
Summary
