🎓 A1Learner
🔍
0 of 46 lessons completed

PowerShell Module Manifests

A module manifest is a data file that describes a module. New-ModuleManifest creates that file, and Test-ModuleManifest reads it back to check it.

New-ModuleManifest creates the manifest file

The code file comes first, then the manifest. Test-Path checks that the manifest file exists.

Set-Content -Path (Join-Path $PSScriptRoot "GreetTools.psm1") -Value 'function Get-Greeting { param([string]$Name = "friend") "Hello, $Name" }'
New-ModuleManifest -Path (Join-Path $PSScriptRoot "GreetTools.psd1") -RootModule "GreetTools.psm1" -ModuleVersion "1.0.0" -Author "Ana" -Description "Greeting helpers." -FunctionsToExport "Get-Greeting"
Test-Path (Join-Path $PSScriptRoot "GreetTools.psd1")

Test-ModuleManifest reads the values back

Test-ModuleManifest reads the manifest back. Each printed value matches what was written.

Set-Content -Path (Join-Path $PSScriptRoot "GreetTools.psm1") -Value 'function Get-Greeting { param([string]$Name = "friend") "Hello, $Name" }'
New-ModuleManifest -Path (Join-Path $PSScriptRoot "GreetTools.psd1") -RootModule "GreetTools.psm1" -ModuleVersion "1.0.0" -Author "Ana" -Description "Greeting helpers." -FunctionsToExport "Get-Greeting"
$m = Test-ModuleManifest -Path (Join-Path $PSScriptRoot "GreetTools.psd1")
$m.Name
$m.Version.ToString()
$m.Author
$m.Description

FunctionsToExport lists the exported functions

Two functions are listed for export. The exported list prints in name order.

Set-Content -Path (Join-Path $PSScriptRoot "GreetTools.psm1") -Value 'function Get-Greeting { param([string]$Name = "friend") "Hello, $Name" }
function Get-Farewell { param([string]$Name = "friend") "Goodbye, $Name" }'
New-ModuleManifest -Path (Join-Path $PSScriptRoot "GreetTools.psd1") -RootModule "GreetTools.psm1" -ModuleVersion "1.0.0" -Author "Ana" -Description "Greeting helpers." -FunctionsToExport "Get-Greeting", "Get-Farewell"
$m = Test-ModuleManifest -Path (Join-Path $PSScriptRoot "GreetTools.psd1")
($m.ExportedFunctions.Keys | Sort-Object) -join ", "

Example

Set-Content -Path (Join-Path $PSScriptRoot "GreetTools.psm1") -Value 'function Get-Greeting { param([string]$Name = "friend") "Hello, $Name" }'
New-ModuleManifest -Path (Join-Path $PSScriptRoot "GreetTools.psd1") -RootModule "GreetTools.psm1" -ModuleVersion "2.1.0" -Author "Ana" -Description "Greeting helpers." -PowerShellVersion "7.0" -FunctionsToExport "Get-Greeting"
$m = Test-ModuleManifest -Path (Join-Path $PSScriptRoot "GreetTools.psd1")
$m.Version.ToString()
Import-Module (Join-Path $PSScriptRoot "GreetTools.psd1")
Get-Greeting -Name "Ana"

Key points

  • Manifest: a .psd1 data file that describes a module.
  • Use New-ModuleManifest to create a manifest for a module.
  • RootModule: the .psm1 file that holds the module code.
  • ModuleVersion: the version number written in the manifest.
  • FunctionsToExport: the list of functions the manifest lets callers use.
  • Use Test-ModuleManifest to read a manifest back and check its values.

In this example, a greeting module is created with a manifest, its version is checked, the module is imported, and the greeting runs. The version prints first, then the greeting.

Note

the manifest holds facts about the module, not runnable code.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which manifest field names the .psm1 file that holds the module code?