PowerShell Module Construction
I’ve created a fair number of PowerShell modules over the last year or so. I’ve experimented with various configurations:
- multiple functions in a single .psm1 file
- single function per file all loaded from the .psm1 file
- modules with and without manifests
- comment based help in various configurations
- external help files
I have standardised on the following:
- single function per file
- comment based help at the end of the function. Its present but its not intrusive
This is the template I use
function aaaa-yyyyyy{
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string]$computer="."
)
BEGIN{}#begin
PROCESS{}#process
END{}#end
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER <Parameter-Name>
.EXAMPLE
.INPUTS
.OUTPUTS
.NOTES
.LINK
#>
}
I’ve put in the CmdletBinding() so I get an advanced function. I’ve added the parameter statement for pipeline input.
The function body is split into BEGIN/PROCESS/END blocks to make me think about the way the function will work
The keywords for the comment based help are present and the comment block has spaces before and after to ensure other comments don’t conflict.
I keep this as a template to load into ISE.
One thing I might add is the other parameter attributes and the validation functions to save looking them up.
Feel free to amend this suit your needs.