Introducing Poshstache: Mustache Templates for PowerShell
Learn how to use Mustache templates in your PowerShell scripts with Poshstache — the most flexible way to generate dynamic text content.
Read more →A PowerShell implementation of Mustache templates — render dynamic content from simple templates using hashtables and objects.
Poshstache is a PowerShell module that implements the Mustache logic-less template specification. It lets you render dynamic text content from simple .mustache (or any text) templates using PowerShell hashtables or objects.
Use Poshstache anywhere you need dynamic text generation: configuration files, HTML emails, deployment scripts, documentation, and more.
Install from the PowerShell Gallery:
# Install for current user
Install-Module -Name Poshstache -Scope CurrentUser
# Install system-wide (requires elevation)
Install-Module -Name Poshstache
# Import after installation
Import-Module Poshstache
Import-Module Poshstache
# Define a template string
$template = "Hello, {{name}}! You have {{count}} new messages."
# Provide data as a hashtable
$data = @{
name = "Alice"
count = 5
}
# Render the template
$result = ConvertTo-PoshstacheTemplate -InputString $template -ParametersObject ($data | ConvertTo-Json)
# Output: "Hello, Alice! You have 5 new messages."
Write-Host $result
# template.mustache
# Dear {{recipient}},
# Your order #{{order_id}} has been {{status}}.
$data = @{
recipient = "Bob Smith"
order_id = "ORD-9821"
status = "shipped"
} | ConvertTo-Json
$result = ConvertTo-PoshstacheTemplate -InputFile "template.mustache" -ParametersObject $data
Write-Output $result
ConvertTo-PoshstacheTemplateRenders a Mustache template with the provided parameters.
| Parameter | Type | Description |
|---|---|---|
-InputString | string | Template content as a string |
-InputFile | string | Path to a template file |
-ParametersObject | string | JSON string with template variables |
| Syntax | Description |
|---|---|
{{variable}} | Renders the value of variable (HTML-escaped) |
{{{variable}}} | Renders the value unescaped |
{{#section}}...{{/section}} | Conditional/loop block |
{{^section}}...{{/section}} | Inverted conditional (renders when falsy) |
{{! comment }} | Comment (not rendered) |
{{> partial}} | Partial template |
$template = @"
Shopping list:
{{#items}}
- {{name}} ({{quantity}})
{{/items}}
"@
$data = @{
items = @(
@{ name = "Apples"; quantity = "6" }
@{ name = "Bread"; quantity = "1 loaf" }
@{ name = "Milk"; quantity = "2 liters" }
)
} | ConvertTo-Json -Depth 5
$result = ConvertTo-PoshstacheTemplate -InputString $template -ParametersObject $data
# appsettings.mustache
# {
# "ConnectionString": "{{db_server}}/{{db_name}}",
# "Environment": "{{env}}",
# "LogLevel": "{{log_level}}"
# }
$config = @{
db_server = "prod-sql.company.com"
db_name = "AppDatabase"
env = "Production"
log_level = "Warning"
} | ConvertTo-Json
ConvertTo-PoshstacheTemplate -InputFile "appsettings.mustache" -ParametersObject $config |
Out-File "appsettings.json" -Encoding utf8
Contributions are welcome! See the GitHub repository for details.
Poshstache is released under the MIT License.