Exploring the Power of New-Module in PowerShell
Welcome back to Wahmans PowerShell blog! Today we’re diving into a lesser-known but incredibly powerful cmdlet: New-Module.
Description:
According to Microsoft:
New-Module creates a new dynamic module that exists only in memory.
This allows us to define reusable code snippets, encapsulate functions, variables, and other module-level elements all within a single command. The beauty of New-Module is that it doesn’t require filesystem access or the overhead of writing and loading external module files. It’s perfect for lightweight scenarios, testing, or even temporary scoped logic.
Example 1: Creating a Simple Function Module (Beginner)
In this example, we create a simple module with a function that says Hello to the user.
$mod = New-Module -ScriptBlock {
function Say-Hello {
param([string]$Name)
"Hello, $Name! Welcome to PowerShell."
}
} -AsCustomObject
$mod.Say-Hello("Alice")
This creates a module in memory, and exposes the Say-Hello function as a method on the resulting object.
Example 2: Private vs Public Functions (Intermediate)
Let’s demonstrate how to create a function that is public (exported) and one that is private (not exported).
$mod = New-Module -ScriptBlock {
function Get-PublicMessage {
Get-PrivateMessage
}
function Get-PrivateMessage {
"This is a private message."
}
Export-ModuleMember -Function Get-PublicMessage
} -AsCustomObject
$mod.Get-PublicMessage
Here, only Get-PublicMessage is available externally. This helps encapsulate implementation details – a key concept in software design!
Example 3: Creating Dynamic Helper Modules (Advanced)
You can build on-the-fly helper libraries like this one that has multiple utilities:
$tools = New-Module -ScriptBlock {
function Convert-ToJsonPretty {
param([object]$Data)
$Data | ConvertTo-Json -Depth 10 -Compress:$false
}
function Get-RandomQuote {
$quotes = @(
"Stay curious.",
"Automate everything.",
"PowerShell is power."
)
Get-Random -InputObject $quotes
}
Export-ModuleMember -Function *
} -AsCustomObject
$tools.Convert-ToJsonPretty -Data @{ Name = 'Wahman'; Lang = 'PowerShell' }
$tools.Get-RandomQuote
This “module” contains two helper functions and can be used just like any static PowerShell module.
Example 4: Dynamic Command Injection (Advanced)
Let’s imagine you need to dynamically generate and inject commands based on runtime logic:
$functionName = "Invoke-Dynamic"
$scriptBlock = {
param($Content)
Write-Host "[Dynamic] $Content"
}
$dynamicModule = New-Module -ScriptBlock {
param($dynFunc, $dynName)
Set-Item -Path function:\$dynName -Value $dynFunc
Export-ModuleMember -Function $dynName
} -ArgumentList $scriptBlock, $functionName -AsCustomObject
$dynamicModule.Invoke-Dynamic("Hello from a runtime-defined function!")
This demonstrates just how powerful New-Module can be in building logic that adapts at runtime.
Conclusion
New-Module is one of those hidden gems in PowerShell that opens up endless possibilities. Whether you want to encapsulate logic, make throwaway reusable tools, or generate code at runtime, this cmdlet is your go-to solution.
Happy scripting, and I will see you in the next post!
Leave a Reply