Guide: How to Publish a PowerShell Module to PowerShell Gallery
This guide explains how to prepare and publish any PowerShell module to the official PowerShell Gallery.
Step 1: Prepare Your Module
- Create a folder for your module, for example:
C:\MyModules\MyModule\
Include your module files:
.psm1- The main module script file..psd1- The module manifest.- Optional:
.ps1xmlfor types,.dllassemblies, or README files.
Create a module manifest using PowerShell:
powershell
New-ModuleManifest -Path "C:\MyModules\MyModule\MyModule.psd1" `
-RootModule "MyModule.psm1" `
-Author "Your Name" `
-Description "Description of your module" `
-Version "1.0.0"
Step 2: Install or Update PowerShellGet
powershell
# Check if PowerShellGet is installed
Get-Module -ListAvailable PowerShellGet
# Update or install if needed
Install-Module -Name PowerShellGet -Force -AllowClobber
Step 3: Create a PowerShell Gallery Account
- Go to PowerShell Gallery and sign up.
- Navigate to My Account → API Keys → Create New Key.
- Copy your API key to use for publishing.
Step 4: Test Your Module Locally
powershell
Import-Module "C:\MyModules\MyModule\MyModule.psd1"
# Test the functions or classes provided by your module
Ensure everything works as expected.
Step 5: Publish the Module
powershell
Publish-Module -Path "C:\MyModules\MyModule" -NuGetApiKey "YOUR_API_KEY"
-Path: Folder containing the module manifest.psd1.-NuGetApiKey: Your API key from PowerShell Gallery.
Note: You may be prompted to install the NuGet provider if publishing for the first time.
Step 6: Verify Publication
Check your module online at PowerShell Gallery Packages.
Step 7: Install Published Module Anywhere
powershell
Install-Module -Name MyModule
Tips
- Versioning: Update the
ModuleVersionin the manifest for new releases. - Test Manifest:
powershell
Test-ModuleManifest "C:\MyModules\MyModule\MyModule.psd1"
- Private Repositories: You can host modules internally using a NuGet feed or file share.
Reference: Use sample structures like
EnvManager.psd1,.psm1, and.types.ps1xmlas examples for proper manifest and module design, but this guide applies to any module.