Creating Modules¶
Creating a PowerShell module is one of the most important steps in moving from “script writer” to “automation engineer.” A module transforms scattered functions into a reusable, versioned, maintainable toolkit. It allows you to share code across teams, publish it internally or publicly, and build automation that scales cleanly.
This section walks you through the complete process of creating a PowerShell module—from folder structure to exporting functions—using professional, real‑world practices.
1. 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 .psm1 file (script module)
- And optionally a .psd1 file (manifest)
Example:
MyModule
│
└── MyModule.psm1
If PowerShell sees this folder in a module path, it recognizes it as a module.
2. 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"
Your folder structure now looks like:
C:\Modules\MyModule
3. 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"
This file will contain your functions.
4. 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"
}
These functions are now part of the module.
5. 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
This ensures:
- Only intended functions are visible
- Helper functions remain private
- The module has a clean, predictable interface
6. 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 "Enrico" `
-ModuleVersion "1.0.0"
This generates a structured metadata file.
Why a manifest matters¶
- Enables versioning
- Required for publishing to repositories
- Controls exports
- Supports dependency management
7. Step 6 — Import and Test the Module¶
Import the module:
Import-Module "C:\Modules\MyModule"
Test the functions:
Get-Greeting -Name "Enrico"
Get-Timestamp
If the functions run, the module is working.
8. 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
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
This structure:
- Keeps the module clean
- Makes maintenance easier
- Supports large codebases
9. Step 8 — Place the Module in a Module Path¶
To make the module available system‑wide, place it in:
-
Current user:
C:\Users\<User>\Documents\PowerShell\Modules -
All users:
C:\Program Files\PowerShell\Modules
Example:
Copy-Item -Path "C:\Modules\MyModule" -Destination "C:\Program Files\PowerShell\Modules" -Recurse
Now you can import it from anywhere:
Import-Module MyModule
10. Summary¶
Creating a PowerShell module involves:
- Creating a folder named after the module
- Adding a
.psm1file with your functions - Exporting the public functions
- Creating a manifest for metadata and versioning
- Organizing code into public/private folders
- 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.