Test-ModuleManifest

Understanding Test-ModuleManifest in PowerShell

Welcome back to Wahmans PowerShell Blog! Today, we’re diving into a powerful cmdlet that doesn’t always get the spotlight it deserves: Test-ModuleManifest. This cmdlet is a great tool for verifying the integrity of PowerShell module manifests. In other words, it checks whether your module’s manifest (.psd1) file correctly describes the module’s contents.

Microsoft describes it as:

Test-ModuleManifest verifies that a module manifest file accurately describes the contents of a module.

Let’s get hands-on with 4 practical examples, moving from beginner to more advanced use cases.

Example 1: Basic Manifest Verification

Suppose you have a PowerShell module named MyCoolModule in a folder. You want to make sure its manifest is valid.

Test-ModuleManifest -Path "C:\Users\Alice\Documents\PowerShell\Modules\MyCoolModule\MyCoolModule.psd1"

This will return an object with details like module version, GUID, author, and a lot more. If the manifest is invalid, PowerShell will throw helpful errors.

Example 2: Detecting Missing Files or Mismatches

If your manifest file specifies files in the NestedModules or ScriptsToProcess sections that aren’t actually present, Test-ModuleManifest will flag it.

Test-ModuleManifest -Path ".\MyModule\MyModule.psd1"

If a file is missing from one of those fields or the RequiredAssemblies don’t exist, it will throw an error indicating exactly what’s wrong.

Example 3: Validate Manifest Before Publishing to Gallery

Before publishing a module to the PowerShell Gallery, it’s a best practice to test the manifest.

$manifest = Test-ModuleManifest -Path ".\MyModule\MyModule.psd1"
$manifest | Format-List

This step ensures all the metadata is valid and adheres to the gallery requirements like Author, CompanyName, and Tags.

Example 4: Programmatic Module Verification in CI/CD Pipelines

For advanced scenarios, such as verifying module manifests automatically in a CI/CD pipeline, you can use the following script to validate across multiple modules:

Get-ChildItem -Path ".\Modules" -Recurse -Filter "*.psd1" |
    ForEach-Object {
        try {
            $result = Test-ModuleManifest -Path $_.FullName
            Write-Host "[$($_.Name)] Manifest is valid."
        }
        catch {
            Write-Warning "[$($_.Name)] Manifest validation failed: $_"
        }
    }

This ensures your module libraries are well-formed and ready for deployment or publishing. It’s also a great quality gate for developers working in teams.

Conclusion

Test-ModuleManifest is a handy cmdlet for any PowerShell module author. Whether you’re verifying correctness, preparing a module for publishing, or maintaining consistency in a team setting, this cmdlet has you covered.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *