BudiBadu Logo
Samplebadu

PowerShell by Example: Module Manifests

PowerShell 7

Creating and managing module metadata with this code example demonstrating New-ModuleManifest, defining dependencies, and exporting module members.

Code

# Create a new module manifest
New-ModuleManifest -Path ".\MyModule\MyModule.psd1" -RootModule "MyModule.psm1" -Author "DevTeam" -Description "Core utilities"

# Validate a manifest file
Test-ModuleManifest -Path ".\MyModule\MyModule.psd1"

# Example Manifest Content (MyModule.psd1)
@{
    # Script module or binary module file associated with this manifest.
    RootModule = 'MyModule.psm1'

    # Version number of this module.
    ModuleVersion = '1.0.0'

    # Modules that must be imported into the global environment prior to importing this module
    RequiredModules = @('Az.Accounts', 'Pester')

    # Functions to export from this module
    FunctionsToExport = @('Get-Widget', 'Set-Widget')

    # URI to the license associated with this module
    LicenseUri = 'https://opensource.org/licenses/MIT'
}

Explanation

A PowerShell Module Manifest (.psd1) is a hash table stored in a text file that describes the contents and attributes of a module. While simple script modules (.psm1) can function alone, a manifest is essential for production-grade code. It provides critical metadata such as the author, version, description, and copyright information. More importantly, it controls how the module is loaded, defining prerequisites like required PowerShell versions, dependent modules, and .NET framework requirements. This ensures that your module fails gracefully if the environment doesn't meet its needs.

The New-ModuleManifest cmdlet is the standard way to generate these files, ensuring the correct structure and encoding. Key properties include RootModule, which points to the primary script or DLL, and FunctionsToExport, which explicitly lists the public API of your module. Explicitly defining exports is a performance best practice; it allows PowerShell to discover commands without loading the entire module, significantly speeding up session startup and command discovery. It also prevents internal helper functions from leaking into the user's session.

Manifests are also the vehicle for publishing to the PowerShell Gallery. Fields like Tags, ProjectUri, and LicenseUri are used by the gallery to index and display your module. Before distribution, it is crucial to validate your manifest using Test-ModuleManifest, which checks for syntax errors and ensures that all referenced files and dependencies are present. Adopting manifests is the first step in moving from ad-hoc scripting to professional PowerShell tool development.

  • Use New-ModuleManifest to generate valid .psd1 files
  • Define FunctionsToExport to improve discovery performance
  • Specify RequiredModules to handle dependencies automatically
  • Validate manifests with Test-ModuleManifest before publishing

Code Breakdown

2
New-ModuleManifest generates a .psd1 file with specified metadata.
5
Test-ModuleManifest verifies the integrity and paths of the manifest.
16
RequiredModules ensures dependencies are loaded before the module.
19
FunctionsToExport limits public commands for better performance.