Menu

1 / 2

When PowerShell Classes and Functions Depend on Each Other — How PSScriptBuilder Handles It

DEV Community·Tim Hartling·6 months ago
#EACFlCtx
Reading 0:00
15s threshold

When you build a PowerShell project from multiple files, the natural structure is clear: enums first, then classes, then functions. Each group has its own place, and as long as dependencies only flow in one direction, that structure works perfectly.

But sometimes a function depends on a class, and that class calls the function. There is no longer a clean boundary between the two groups — they need to be interleaved in the output. That's a cross-dependency, and handling it manually is where things get fragile.

What a Cross-Dependency Looks Like

Consider this scenario:

# New-Report.ps1 function New-Report ([ ReportConfig ] $config ) { # uses ReportConfig as a parameter type }

# ReportConfig.ps1 class ReportConfig { [ string ] $Title

[ void ] Generate () { New-Report $this # calls New-Report } }

Enter fullscreen mode

Exit fullscreen mode

New-Report references ReportConfig as a parameter type — so ReportConfig must be defined before New-Report is loaded.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.