├── Module-Name ├── Module-Name.psd1 ├── Module-Name.psm1 ├── Private │ ├── Config.yaml │ └── Private-Function.ps1 ├── Public │ └── Public-Function.ps1 └── Tests │ └── Module-Name.Tests.ps1 ├── New-PSModule.ps1 ├── README.md └── _Templates ├── Module.Tests.ps1 └── Module.psm1 /Module-Name/Module-Name.psd1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rokett/Powershell-Module-Template/aa55baa7b39764bb555673c9379439483ba72e15/Module-Name/Module-Name.psd1 -------------------------------------------------------------------------------- /Module-Name/Module-Name.psm1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rokett/Powershell-Module-Template/aa55baa7b39764bb555673c9379439483ba72e15/Module-Name/Module-Name.psm1 -------------------------------------------------------------------------------- /Module-Name/Private/Config.yaml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rokett/Powershell-Module-Template/aa55baa7b39764bb555673c9379439483ba72e15/Module-Name/Private/Config.yaml -------------------------------------------------------------------------------- /Module-Name/Private/Private-Function.ps1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rokett/Powershell-Module-Template/aa55baa7b39764bb555673c9379439483ba72e15/Module-Name/Private/Private-Function.ps1 -------------------------------------------------------------------------------- /Module-Name/Public/Public-Function.ps1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rokett/Powershell-Module-Template/aa55baa7b39764bb555673c9379439483ba72e15/Module-Name/Public/Public-Function.ps1 -------------------------------------------------------------------------------- /Module-Name/Tests/Module-Name.Tests.ps1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rokett/Powershell-Module-Template/aa55baa7b39764bb555673c9379439483ba72e15/Module-Name/Tests/Module-Name.Tests.ps1 -------------------------------------------------------------------------------- /New-PSModule.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Generates the folder and file structure needed to create a new Powershell module. 4 | 5 | .DESCRIPTION 6 | In order to provide some standardisation, this script will create the necessary folder/file structure for a new Powershell module. 7 | 8 | The structure includes folders for Public and Private functions, creates a basic manifest file and root module loader, and creates a Pester test file for the manifest. 9 | 10 | Once the environment is setup, proper development can begin. Any public functions to be exported should still be added to the FunctionsToExport array in the manifest file. 11 | 12 | Private functions are those which are internal to the module and are therefore not for public consumption. 13 | 14 | Each public function should have its own test file in the Tests folder. 15 | 16 | .PARAMETER ModuleName 17 | The name you wish to give the module. The root folder, manifest, and root loader will be named after the module. 18 | 19 | .PARAMETER Author 20 | Enter a name to be listed as the Author in the module manifest. 21 | 22 | .PARAMETER Description 23 | A short description of the module to be listed in the module manifest. 24 | 25 | .PARAMETER PowershellVersion 26 | The minimum version of Powershell supported by the module. One of 2.0, 3.0 (the default), 4.0 or 5.0. 27 | 28 | .PARAMETER ModulesPath 29 | The full path to the directory you wish to develop the module in. This is where the module structure will be created. 30 | Include a trailing \ or don't, it doesn't matter. 31 | 32 | .EXAMPLE 33 | New-PSModule.ps1 -ModuleName WackyRaces -Author 'Penelope Pitstop' -Description 'Win the wacky races' -PowershellVersion '4.0' -ModulesPath 'c:\development\powershell-modules' 34 | Creates a new module structure called WackyRaces in c:\development\powershell-modules\WackyRaces. The module manifest will require Powershell v4.0. 35 | #> 36 | 37 | [CmdletBinding()] 38 | Param( 39 | [Parameter(Mandatory=$True)] 40 | [string]$ModuleName, 41 | 42 | [Parameter(Mandatory=$True)] 43 | [string]$Author, 44 | 45 | [Parameter(Mandatory=$True)] 46 | [string]$Description, 47 | 48 | [Parameter(Mandatory=$False)] 49 | [ValidateSet('2.0','3.0','4.0','5.0','6.0','7.0')] 50 | [string]$PowershellVersion = '7.0', 51 | 52 | [Parameter(Mandatory=$True)] 53 | [System.IO.FileInfo]$ModulesPath 54 | ) 55 | 56 | $rootLoaderTemplate = Join-Path $PSScriptRoot '_Templates' 'Module.psm1' 57 | $manifestTestFileTemplate = Join-Path $PSScriptRoot '_Templates' 'Module.Tests.ps1' 58 | 59 | $moduleDir = Join-Path $ModulesPath.FullName $ModuleName 60 | $publicDir = Join-Path $moduleDir 'Public' 61 | $privateDir = Join-Path $moduleDir 'Private' 62 | $testsDir = Join-Path $moduleDir 'Tests' 63 | 64 | New-Item -Path $moduleDir -Type Directory 65 | New-Item -Path $publicDir -Type Directory 66 | New-Item -Path $privateDir -Type Directory 67 | New-Item -Path $testsDir -Type Directory 68 | 69 | $manifestTestFile = Join-Path $testsDir "$ModuleName.Tests.ps1" 70 | $manifestFile = Join-Path $moduleDir "$ModuleName.psd1" 71 | $rootLoader = Join-Path $moduleDir "$ModuleName.psm1" 72 | 73 | $newModuleManifestParams = @{ 74 | Path = $ManifestFile 75 | Author = $Author 76 | Description = $Description 77 | PowershellVersion = $PowershellVersion 78 | RootModule = $ModuleName 79 | } 80 | 81 | New-ModuleManifest @newModuleManifestParams 82 | 83 | Copy-Item -Path $rootLoaderTemplate -Destination $rootLoader 84 | Copy-Item -Path $manifestTestFileTemplate -Destination $manifestTestFile -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Module-Name 2 | This is an example folder structure for a new PowerShell module. 3 | 4 | ### _Templates 5 | Files in here are needed to run the New-PSModule.ps1 script to create a new module. 6 | 7 | ### New-PSModule.ps1 8 | A script to run which can be used to create new modules. -------------------------------------------------------------------------------- /_Templates/Module.Tests.ps1: -------------------------------------------------------------------------------- 1 | $ModulePath = Split-Path -Path $PSScriptRoot -Parent 2 | $ModuleName = Split-Path -Path $ModulePath -Leaf 3 | 4 | # Make sure one or multiple versions of the module are not loaded 5 | Get-Module -Name $ModuleName | Remove-Module 6 | 7 | # Import the module and store the information about the module 8 | $ModuleInformation = Import-Module -Name "$ModulePath\$ModuleName.psd1" -PassThru 9 | $ModuleInformation | Format-List 10 | 11 | # Get the functions present in the Manifest 12 | $ExportedFunctions = $ModuleInformation.ExportedFunctions.Values.Name 13 | 14 | # Get the functions present in the Public folder 15 | $PS1Functions = Get-ChildItem -Path "$ModulePath\Public\*.ps1" 16 | 17 | Describe "$ModuleName Module - Testing Manifest File (.psd1)" { 18 | Context "Manifest" { 19 | It "Should contain RootModule" { 20 | $ModuleInformation.RootModule | Should Not BeNullOrEmpty 21 | } 22 | 23 | It "Should contain ModuleVersion" { 24 | $ModuleInformation.Version | Should Not BeNullOrEmpty 25 | } 26 | 27 | It "Should contain GUID" { 28 | $ModuleInformation.Guid | Should Not BeNullOrEmpty 29 | } 30 | 31 | It "Should contain Author" { 32 | $ModuleInformation.Author | Should Not BeNullOrEmpty 33 | } 34 | 35 | It "Should contain Description" { 36 | $ModuleInformation.Description | Should Not BeNullOrEmpty 37 | } 38 | 39 | It "Compare the count of Function Exported and the PS1 files found" { 40 | $status = $ExportedFunctions.Count -eq $PS1Functions.Count 41 | $status | Should Be $true 42 | } 43 | 44 | It "Compare the missing function" { 45 | If ($ExportedFunctions.count -ne $PS1Functions.count) { 46 | $Compare = Compare-Object -ReferenceObject $ExportedFunctions -DifferenceObject $PS1Functions.Basename 47 | $Compare.InputObject -Join ',' | Should BeNullOrEmpty 48 | } 49 | } 50 | } 51 | } 52 | 53 | Get-Module -Name $ModuleName | Remove-Module -------------------------------------------------------------------------------- /_Templates/Module.psm1: -------------------------------------------------------------------------------- 1 | $scriptRoot = $PSScriptRoot + '\public' 2 | 3 | Get-ChildItem $scriptRoot *.ps1 | ForEach-Object { 4 | Import-Module $_.FullName 5 | } --------------------------------------------------------------------------------