PowerShell function for creating a script module template

I'm curious to know what process others use to create new PowerShell script modules?

Since the initial process of creating a PowerShell script module seems redundant and tedious, I decided to create a function that creates a template for new script modules that I create which includes creating both the script module PSM1 and manifest PSD1 files and filling in the information that I would normally include:

 1function New-MrScriptModule {
 2
 3<#
 4.SYNOPSIS
 5    Creates a new PowerShell script module in the specified location.
 6
 7.DESCRIPTION
 8    New-MrScriptModule is an advanced function that creates a new PowerShell script module in the
 9    specified location including creating the module folder and both the PSM1 script module file
10    and PSD1 module manifest.
11
12.PARAMETER Name
13    Name of the script module.
14
15.PARAMETER Path
16    Parent path of the location to create the script module in. This location must already exist.
17
18.PARAMETER Author
19    Specifies the module author.
20
21.PARAMETER CompanyName
22    Identifies the company or vendor who created the module.
23
24.PARAMETER Description
25    Describes the contents of the module.
26
27.PARAMETER PowerShellVersion
28    Specifies the minimum version of Windows PowerShell that will work with this module. For example,
29    you can enter 3.0, 4.0, or 5.0 as the value of this parameter.
30
31.EXAMPLE
32     New-MrScriptModule -Name MyModuleName -Path "$env:ProgramFiles\WindowsPowerShell\Modules" -Author 'Mike F Robbins' -CompanyName mikefrobbins.com -Description 'Brief description of my PowerShell module' -PowerShellVersion 3.0
33
34.INPUTS
35    None
36
37.OUTPUTS
38    None
39
40.NOTES
41    Author:  Mike F Robbins
42    Website: http://mikefrobbins.com
43    Twitter: @mikefrobbins
44#>
45
46    [CmdletBinding()]
47    param (
48        [Parameter(Mandatory)]
49        [string]$Name,
50
51        [ValidateScript({
52          If (Test-Path -Path $_ -PathType Container) {
53            $true
54          }
55          else {
56            Throw "'$_' is not a valid directory."
57          }
58        })]
59        [String]$Path,
60
61        [Parameter(Mandatory)]
62        [string]$Author,
63
64        [Parameter(Mandatory)]
65        [string]$CompanyName,
66
67        [Parameter(Mandatory)]
68        [string]$Description,
69
70        [Parameter(Mandatory)]
71        [string]$PowerShellVersion
72    )
73
74    New-Item -Path $Path -Name $Name -ItemType Directory | Out-Null
75    Out-File -FilePath "$Path\$Name\$Name.psm1" -Encoding utf8
76    Add-Content -Path "$Path\$Name\$Name.psm1" -Value '#Dot source all functions in all ps1 files located in the module folder
77Get-ChildItem -Path $PSScriptRoot\*.ps1 -Exclude *.tests.ps1, *profile.ps1 |
78ForEach-Object {
79    . $_.FullName
80}'
81    New-ModuleManifest -Path "$Path\$Name\$Name.psd1" -RootModule $Name -Author $Author -Description $Description -CompanyName $CompanyName `
82    -PowerShellVersion $PowerShellVersion -AliasesToExport $null -FunctionsToExport $null -VariablesToExport $null -CmdletsToExport $null
83}

When the previous function is run, a folder with the specified module name is created along with the PSM1 and PSD1 files. The PSM1 is set to dot source any function that exists in the module's folder. I could have easily coded the specific user and all users paths for modules into the function, but I rarely develop new modules in either of those locations.

1New-MrScriptModule -Name MyModuleName -Path "$env:ProgramFiles\WindowsPowerShell\Modules" -Author 'Mike F Robbins' -CompanyName mikefrobbins.com -Description 'Brief description of my PowerShell module' -PowerShellVersion 3.0

module-template1a.jpg

The function shown in this blog article is a work in progress and can be downloaded from my PowerShell repository on GitHub. Feel free to fork the repository and submit pull requests if you can think of improvements that could be made to the code shown in this blog article.

µ