Functions/Create-NewModuleStructure.ps1

Function Add-NewModuleStructure {
    <#
        .SYNOPSIS
            Creates a new PowerShell module structure with standard folders and files
 
        .DESCRIPTION
            This function creates a complete PowerShell module structure including folders for Functions, Helpers, Tests, and WIP.
            It also generates the module manifest (.psd1), module file (.psm1), and help files with proper initialization.
 
        .PARAMETER Path
            The path to where you want the module structure created. Default is C:\Temp
 
        .PARAMETER ModuleName
            The name of the module you are creating (required)
 
        .PARAMETER Author
            The author name for the module manifest. Default is "Lars Panzerbjørn"
 
        .PARAMETER GitHub
            The GitHub username for the module. Default is "Panzerbjrn"
 
        .PARAMETER Description
            The description for the module you are creating. Default is 'New PowerShell module'
 
        .EXAMPLE
            Create-NewModuleStructure -Path C:\Temp\ -ModuleName Test-Module -Author LarsP -Description "Test PowerShell Module"
 
            Creates a new module structure at C:\Temp\Test-Module
 
        .EXAMPLE
            Create-NewModuleStructure -ModuleName ServiceNowCMDB -Path C:\Temp -Description "Helper Functions to work with ServiceNow's CMDB"
 
            Creates the ServiceNowCMDB module structure with custom description
 
        .EXAMPLE
            Add-NewModuleStructure -ModuleName MyModule
 
            Creates a new module structure with default settings at C:\Temp\MyModule
 
        .INPUTS
            None
 
        .OUTPUTS
            None
 
        .NOTES
            Author: Lars Panzerbjrn
            Creation Date: 2018.11.08
 
    #>

    [CmdletBinding(PositionalBinding = $False)]
    [Alias('Create-NewModuleStructure')]
    param(
        [Parameter()][string]$Author = "Lars Panzerbjørn",
        [Parameter(Mandatory = $True)][string]$ModuleName,
        [Parameter()][string]$Path = "C:\Temp",
        [Parameter()][string]$GitHub = "Panzerbjrn",
        [Parameter()][string]$Description = 'New PowerShell module'
    )

    BEGIN{
        $Date = Get-Date -f yyyy.MM.dd
        Write-Verbose "Path is $Path"
        $PSMContent = "#region Script Header
# NAME: $($ModuleName).psm1
# AUTHOR: $($Author)
# GitHub: $($GitHub)
# DATE: $($Date)
# VERSION: 0.1 - $($Date) - Module Created with Create-NewModuleStructure by Lars Panzerbjørn
#
# SYNOPSIS:
#
#
# DESCRIPTION:
# $($Description)
#
# REQUIREMENTS:
#
#endregion Script Header
 
#Requires -Version 5.0
 
[CmdletBinding(PositionalBinding=`$False)]
param()
 
Write-Verbose `$PSScriptRoot
 
#Get Functions and Helpers function definition files.
`$Functions = @( Get-ChildItem -Path `$PSScriptRoot\Functions\*.ps1 -ErrorAction SilentlyContinue )
`$Helpers = @( Get-ChildItem -Path `$PSScriptRoot\Helpers\*.ps1 -ErrorAction SilentlyContinue )
 
#Dot source the files
ForEach (`$Import in @(`$Functions + `$Helpers)){
    TRY{
        . `$Import.Fullname
    }
    CATCH{
        Write-Error -Message `"Failed to Import function `$(`$Import.Fullname): `$_`"
    }
}
 
Export-ModuleMember -Function `$Functions.Basename
"

        $AboutHelpContent = "TOPIC
        about_$($ModuleName)
 
SHORT DESCRIPTION
        Default: A short, one-line description of the topic contents.
 
LONG DESCRIPTION
        Default: A detailed, full description of the subject or purpose of the module.
 
EXAMPLES
        Default: Examples of how to use the module or how the subject feature works in practice.
 
KEYWORDS
        Default: Terms or titles on which you might expect your users to search for the information in this topic.
 
SEE ALSO
        Default: Text-only references for further reading. Hyperlinks cannot work in the Windows PowerShell console.
"

    }

    PROCESS{
        $Path = Join-Path $Path $ModuleName
        Write-Verbose "Creating the module and function directories"
        IF(!(Test-Path -Path ($Path))) { New-Item ($Path) -ItemType Directory -Force }
        IF(!(Test-Path -Path ($Path + "\Helpers"))) { New-Item ($Path + "\Helpers") -ItemType Directory -Force } # For private/Helper functions that should not be exposed to end users
        IF(!(Test-Path -Path ($Path + "\Functions"))) { New-Item ($Path + "\Functions") -ItemType Directory -Force } # For public/exported functions
        IF(!(Test-Path -Path ($Path + "\en-GB"))) { New-Item ($Path + "\en-GB") -ItemType Directory -Force } # For English about_Help files
        IF(!(Test-Path -Path ($Path + "\WIP"))) { New-Item ($Path + "\WIP") -ItemType Directory -Force } # For Functions that are Works in Progress
        IF(!(Test-Path -Path ($Path + "\Tests"))) { New-Item ($Path + "\Tests") -ItemType Directory -Force } # For Pester tests

        #Create the module and related files
        Write-Verbose "Creating the module and function files"
        IF(!(Test-Path -Path "$Path\$ModuleName.psm1")) { New-Item "$Path\$ModuleName.psm1" -ItemType File }
        IF(!(Test-Path -Path "$Path\en-GB\about_$ModuleName.help.txt")) { New-Item "$Path\en-GB\about_$ModuleName.help.txt" -ItemType File }
        IF(!(Test-Path -Path "$Path\Tests\$ModuleName.Tests.ps1")) { New-Item "$Path\Tests\$ModuleName.Tests.ps1" -ItemType File }

        Write-Verbose "Creating the module manifest"
        Write-Verbose "Path is $($Path+"\"+$ModuleName+".psd1")"
        Write-Verbose "RootModule is $($ModuleName.psm1)"
        Write-Verbose "Description is $($Description)"
        Write-Verbose "Author is $($Author)"
        $PSD1Path = ($Path + "\" + $ModuleName + ".psd1")
        $RootModule = $ModuleName.psm1
        $Description = $Description
        $PowerShellVersion = "5.0"
        $Author = $Author
        Write-Verbose "Path is $Path"
        Write-Verbose "RootModule is $RootModule"
        Write-Verbose "Description is $Description"
        Write-Verbose "PowerShellVersion is $PowerShellVersion"
        Write-Verbose "Author is $Author"
        New-ModuleManifest -Path $PSD1Path -Author $Author -RootModule $RootModule -Description $Description -PowerShellVersion $PowerShellVersion
        $PSMContent | Add-Content "$Path\$ModuleName.psm1" -Force
        $AboutHelpContent | Add-Content "$Path\en-GB\about_$ModuleName.help.txt" -Force
        # Put the Public Functions/exported functions into the Functions folder and Helper functions into Helpers folder
    }
}