Skip to content
View PSScriptBuilder's full-sized avatar

Block or report PSScriptBuilder

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Maximum 250 characters. Please don’t include any personal information such as legal names or email addresses. Markdown is supported. This note will only be visible to you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
PSScriptBuilder/README.md

PSScriptBuilder

PSScriptBuilder

PowerShell Gallery Downloads Platform

powershell License: MIT website documentation

A dependency-aware PowerShell script builder that combines multi-file projects into a single, deployable script.

The Problem

As a PowerShell project grows, managing the order of classes, enums, and functions across dozens of files becomes error-prone and hard to maintain. Class and enum definitions must appear before the code that references them — base classes before derived classes, enums before the classes that use them, classes before the functions that depend on them.

The Solution

PSScriptBuilder analyzes your source files using PowerShell's AST, resolves all dependencies automatically, and combines everything into a single, correctly ordered, deployable script — no manual sorting required.

Features

  • Automatic Dependency Ordering — AST analysis resolves class inheritance and type dependencies; output is always in valid load order
  • Cycle Detection — Detects fatal circular dependencies (inheritance, static initializers) before any output is written; type reference cycles are resolved automatically
  • Cross-Dependency Support — Detects when classes and functions must be interleaved and switches to a unified ordered output block automatically
  • Flexible Templates — Define your output structure once using a template with token placeholders; supports any output format (.psm1, .ps1, .txt)
  • Multiple Collectors — Dedicated collectors for Classes, Functions, Enums, Using statements, and raw Files
  • Project Scaffolding — Generates a ready-to-build project structure with sample source files and a build script
  • Script Compression — Strips comments and blank lines from the built output for lean, deployable scripts
  • Release Management — Built-in SemVer version bumping, Git metadata extraction, and file update automation
  • PowerShell 5.1 and 7+ Compatible — Runs on Windows PowerShell 5.1 and PowerShell 7+ (Windows, Linux, macOS)

Getting Started

PSScriptBuilder is available on the PowerShell Gallery and requires using module for import.

1. Install

Install-Module -Name PSScriptBuilder -Scope CurrentUser

2. Import

Note: Always use using module — not Import-Module. PowerShell only loads class and enum definitions with using module. The using statement must appear at the very top of your script, before any other code.

✅ Correct:

using module PSScriptBuilder

❌ Wrong — classes and enums will NOT be available:

Import-Module PSScriptBuilder

Quick Start

After importing the module, follow the path that matches your situation.


New Project

1. Scaffold

New-PSScriptBuilderProject -Name "MyProject" -Path "C:\Projects"
cd "C:\Projects\MyProject"

2. Build (runs the generated build script with sample source files)

.\Build-MyProject.ps1

Existing Project

1. Create configuration (first-time setup)

New-PSScriptBuilderConfiguration

2. Configure collectors

$contentCollector = New-PSScriptBuilderContentCollector |
    Add-PSScriptBuilderCollector -Type Class    -IncludePath "src/Classes" |
    Add-PSScriptBuilderCollector -Type Function -IncludePath "src/Public"

3. Build

Invoke-PSScriptBuilderBuild `
    -ContentCollector $contentCollector `
    -TemplatePath     "build/MyModule.psm1.template" `
    -OutputPath       "build/Output/MyModule.psm1"

Note: PSScriptBuilder resolves relative paths (like src/Classes) against the project root. Run these commands from your project directory, or call Set-PSScriptBuilderProjectRoot -Path "C:\Projects\MyProject" first to set it explicitly.


Examples

Fourteen runnable examples are included in the examples/ directory, covering scenarios from simple function builds to full release workflows with scaffolding and compression.

See the Examples page for descriptions and walkthroughs.

Collector Types

PSScriptBuilder provides five collector types, each targeting a specific PowerShell component.

Type Description
Using Collects using statements
Enum Collects enumeration definitions
Class Collects class definitions with dependency order
Function Collects standalone function definitions with dependency order
File Includes complete file contents as-is

Each collector accepts -IncludePath, -ExcludePath, -IncludeFile, -ExcludeFile, -FileExtension, and an optional -CollectionKey for use as a template token.

See the Collectors Guide for details on filtering, custom keys, and advanced collector configuration.

Template System

PSScriptBuilder uses plain text template files with {{Token}} placeholders to define the structure of the output file — supporting any extension (.psm1, .ps1, .txt, etc.).

Default CollectionKey per collector type:

Collector Type Default Token
Using {{USING_STATEMENTS}}
Enum {{ENUM_DEFINITIONS}}
Class {{CLASS_DEFINITIONS}}
Function {{FUNCTION_DEFINITIONS}}
File {{FILE_CONTENTS}}

Example template (build/MyModule.psm1.template):

{{USING_STATEMENTS}}

{{ENUM_DEFINITIONS}}

{{CLASS_DEFINITIONS}}

{{FUNCTION_DEFINITIONS}}

{{FILE_CONTENTS}}

Use -CollectionKey on Add-PSScriptBuilderCollector to define a custom token name, e.g. {{DOMAIN_CLASSES}}.

Ordered and Hybrid Mode

When classes and functions have mutual dependencies that require interleaving in the output, PSScriptBuilder automatically switches to Ordered Mode. Replace the individual per-type placeholders with a single {{ORDERED_COMPONENTS}} token:

{{USING_STATEMENTS}}
{{ORDERED_COMPONENTS}}

Hybrid Mode lets you adopt this layout proactively — even when no cross-dependencies currently exist — making the template resilient to future dependency changes without restructuring. See the Templates Guide for details.

Release Management

PSScriptBuilder includes cmdlets for SemVer version management, Git metadata extraction, and automated file updates.

# Bump the version (Major / Minor / Patch)
Update-PSScriptBuilderReleaseData -Minor

# Propagate version tokens across all registered files
Update-PSScriptBuilderBumpFiles

# Then run your build
Invoke-PSScriptBuilderBuild `
    -ContentCollector $contentCollector `
    -TemplatePath     "build/MyModule.psm1.template" `
    -OutputPath       "build/Output/MyModule.psm1"

See the Release Management Guide for details on bump configuration, token replacement, and the full release pipeline.

Build from Source

To build the module from source, clone the repository and run the build script.

git clone https://github.com/PSScriptBuilder/PSScriptBuilder.git
cd PSScriptBuilder

.\build.ps1 -ProjectRoot $PWD

build.ps1 is the bootstrap script — it does not require PSScriptBuilder to be installed and is used for the initial build only. Once the module is built, Build-Module.ps1 is used for subsequent builds.

Contributing

This GitHub repository is a mirror of the primary development repository hosted on GitLab. Pull requests submitted here will not be merged. Please use GitHub Issues for bug reports and feature requests.

For code conventions, testing instructions, and contribution guidelines, see CONTRIBUTING.md.

See Also

ModuleBuilder — a simpler alternative for projects that do not require class dependency ordering.

Changelog

See CHANGELOG.md.

License

MIT — see LICENSE.

Author

Tim Hartling

For questions, bug reports, or feature requests, please open an Issue on GitHub.

Popular repositories Loading

  1. PSScriptBuilder PSScriptBuilder Public

    PowerShell 5.1+ script builder for multi-file projects. Features: class/enum/function collection, automatic dependency resolution, topological sorting, circular dependency detection, template-based…

    PowerShell 1

  2. PSScriptBuilder-Docs PSScriptBuilder-Docs Public

    Documentation site for PSScriptBuilder — hosted at docs.psscriptbuilder.com

    HTML