PowerShell Module Distribution
Module distribution is how a module reaches other machines. A release folder holds the module files in a version folder, ready to be copied or published.
A release folder holds the module files
The release folder holds MathTools version 1.0.0. Its two files, sorted by name, are the whole release.
$releaseRoot = Join-Path $PSScriptRoot "Release"
$modDir = Join-Path $releaseRoot "MathTools"
$versionDir = Join-Path $modDir "1.0.0"
New-Item -ItemType Directory -Force -Path $versionDir | Out-Null
Set-Content -Path (Join-Path $versionDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Set-Content -Path (Join-Path $versionDir "MathTools.psd1") -Value '@{ ModuleVersion = "1.0.0"; RootModule = "MathTools.psm1"; FunctionsToExport = @("Add-Numbers") }'
Get-ChildItem -Path $versionDir -File | Sort-Object Name | Select-Object -ExpandProperty NameThe release copy still loads and works
Importing the manifest from the release folder loads the release copy. Its function gives the same answer as the source copy.
$releaseRoot = Join-Path $PSScriptRoot "Release"
$modDir = Join-Path $releaseRoot "MathTools"
$versionDir = Join-Path $modDir "1.0.0"
New-Item -ItemType Directory -Force -Path $versionDir | Out-Null
Set-Content -Path (Join-Path $versionDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Set-Content -Path (Join-Path $versionDir "MathTools.psd1") -Value '@{ ModuleVersion = "1.0.0"; RootModule = "MathTools.psm1"; FunctionsToExport = @("Add-Numbers") }'
Import-Module (Join-Path $versionDir "MathTools.psd1")
Add-Numbers -a 3 -b 4The manifest states the release version
The manifest line for the version is read back from the release file. It must match the version folder name.
$releaseRoot = Join-Path $PSScriptRoot "Release"
$modDir = Join-Path $releaseRoot "MathTools"
$versionDir = Join-Path $modDir "1.0.0"
New-Item -ItemType Directory -Force -Path $versionDir | Out-Null
Set-Content -Path (Join-Path $versionDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Set-Content -Path (Join-Path $versionDir "MathTools.psd1") -Value '@{ ModuleVersion = "1.0.0"; RootModule = "MathTools.psm1"; FunctionsToExport = @("Add-Numbers") }'
Set-Content -Path (Join-Path $versionDir "MathTools.psd1") -Value @("@{", ' ModuleVersion = "1.0.0";', ' RootModule = "MathTools.psm1";', ' FunctionsToExport = @("Add-Numbers");', "}")
Get-Content -Path (Join-Path $versionDir "MathTools.psd1") | Where-Object { $_ -match "ModuleVersion" }Example
$sourceDir = Join-Path $PSScriptRoot "Source/MathTools"
New-Item -ItemType Directory -Force -Path $sourceDir | Out-Null
Set-Content -Path (Join-Path $sourceDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Set-Content -Path (Join-Path $sourceDir "MathTools.psd1") -Value '@{ ModuleVersion = "1.0.0"; RootModule = "MathTools.psm1"; FunctionsToExport = @("Add-Numbers") }'
$versionDir = Join-Path $PSScriptRoot "Release/MathTools/1.0.0"
New-Item -ItemType Directory -Force -Path $versionDir | Out-Null
Copy-Item -Path (Join-Path $sourceDir "MathTools.psm1") -Destination $versionDir
Copy-Item -Path (Join-Path $sourceDir "MathTools.psd1") -Destination $versionDir
Get-ChildItem -Path $versionDir -File | Sort-Object Name | Select-Object -ExpandProperty Name
Import-Module (Join-Path $versionDir "MathTools.psd1")
Add-Numbers -a 3 -b 4Key points
- Module distribution: how a module reaches other machines.
- Use a release folder to hold the module files in a version folder.
- A release folder must hold both the module code file and the manifest file.
- Use the version folder name to state which version the release holds.
- Publishing to a gallery is a separate step that uploads the release folder for others to install.
In this example, the block builds the source module, then copies both of its files into a release folder under version 1.0.0. The file list proves both files travelled, and importing the release copy and calling its function proves the release works.
Note
on this platform there is no gallery connection, so the gallery steps in this lesson are described, not run.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does a release folder hold for other machines?