PowerShellGet and the PowerShell Gallery are the equivalent of NuGet/NPM/Bower package managers for PowerShell. They come handy to share scripts and modules.
Installing a module, like this demo one for instance, is a one-liner:
Creating and publishing a new module is also relatively straightforward, although not exceptionally well documented.
A simple Write-HelloWorld
cmdlet consists in a class deriving from Cmdlet
that overrides BeginProcessing
to output a string:
One trick here is finding the System.Management.Automation
reference. On my machine (Windows 10 x64) it could be found at c:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll
.
Once compiled, the DLL that can be loaded in PowerShell using Import-Module
.
To make it a publishable module requires adding a .psd1 PowerShell manifest akin to NuGet’s .nuspec XML manifest:
Both the DLL and .psd1 file need to be placed in a folder whose name is the package name. Once this is done, the package gets uploaded to the gallery using
where the API Key comes from https://powershellgallery.com/account.
That is it. For the full source code and some unit-testing tricks see this repo on GitHub.