Skip to content

Module Structure

PowerShell modules are foundational for reusable, maintainable automation in enterprise environments. A well-structured module ensures clarity, security, and compatibility across systems. This section outlines the core components of a PowerShell module, best practices for organization, and strategies for enterprise-grade development.


Module Components

1. Module Manifest (*.psd1)

The manifest file defines metadata and exports for the module. It must reside in the module's root directory and use the .psd1 extension. Key properties include:
- RootModule: Specifies the primary module file (e.g., MyModule.psm1).
- ModuleVersion: Follow semantic versioning (e.g., 1.0.0).
- GUID: A unique identifier for the module (generate using New-Guid).
- FunctionsToExport/CmdletsToExport: Explicitly define what is exposed.

Example manifest snippet:

@{
    RootModule = 'MyModule.psm1'
    ModuleVersion = '1.0.0'
    GUID = 'a1b2c3d4-e5f6-7890-g1h2-i3j4k5l6m7n8'
    FunctionsToExport = 'Get-Data', 'Set-Config'
    CmdletsToExport = ''
    NestedModules = 'NestedModule.psm1'
}

2. Module File (*.psm1)

Contains the actual PowerShell code (functions, cmdlets, variables). This file is loaded when the module is imported. Example:

function Get-Data {
    return "Hello, World!"
}

function Set-Config {
    param ([string]$Path)
    Set-Item -Path $Path -Value "Enabled"
}

3. Exports

Use Export-ModuleMember to control what is available to users. Explicit exports prevent accidental exposure of internal functions:

Export-ModuleMember -Function Get-Data, Set-Config


Folder Structure for Enterprise Use

Organize modules into a structured directory to support scalability and collaboration:

MyModule/
├── MyModule.psm1          # Main module code
├── MyModule.psd1          # Manifest file
├── src/                   # Source code (optional)
├── tests/                 # Unit tests (e.g., Pester scripts)
├── examples/              # Usage examples
├── README.md              # Documentation
└── bin/                   # Compiled binaries (if applicable)

  • src/: For organizing functions into submodules or categories.
  • tests/: Include Pester tests for validation.
  • examples/: Provide real-world usage scenarios.
  • README.md: Document installation, usage, and dependencies.

Best Practices

  1. Semantic Versioning
    Use Major.Minor.Patch format for ModuleVersion to track changes and ensure backward compatibility.

  2. Unique GUID
    Generate a GUID for each module to avoid conflicts.

  3. Compatibility
    Specify $PSVersionTable requirements in the manifest to ensure compatibility with target environments.

  4. Security
    Sign modules with a certificate and restrict exports to minimize attack surfaces.

  5. Testing
    Integrate automated testing (e.g., Pester) and CI/CD pipelines for validation.

  6. Documentation
    Maintain clear, up-to-date documentation in README.md and examples.


Key takeaways

  • Structure: Always include a .psd1 manifest and .psm1 code file.
  • Exports: Use Export-ModuleMember to control visibility.
  • Organization: Use subdirectories (src, tests, etc.) for scalability.
  • Security: Sign modules and limit exposed functions.
  • Versioning: Adopt semantic versioning for clarity and compatibility.