powershell
napt.powershell
PowerShell string quoting and file encoding for generated scripts.
Values written into PowerShell source (recipe fields, installer metadata, file paths) can be vendor-controlled, and a value that closes its string early runs as code on the endpoint or the build host.
PowerShell treats typographic quotes as string delimiters too: U+2018 to U+201B close a single-quoted string, U+201C to U+201E a double-quoted one. Both quoting functions escape the full sets. A value that lands outside a string, such as an app name in a comment line, needs a different guard: a line break ends the comment and the rest of the value runs as code, so strip_control_characters removes line breaks and the other control characters first.
ps_single_quote
Formats a value as a single-quoted PowerShell string literal.
Single-quoted strings are verbatim: no variable expansion, no subexpressions, no backtick escapes. The only characters with meaning are the quote delimiters, which are escaped by doubling. Prefer this form whenever the surrounding PowerShell allows it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw text to embed. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The literal including its surrounding quotes. |
Source code in napt/powershell.py
ps_escape_double_quoted
Escapes a value for use inside a double-quoted PowerShell string.
Backticks, dollar signs, and every double-quote delimiter are prefixed
with a backtick so the value reads as literal text instead of closing
the string, expanding a variable, or running a $(...) subexpression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw text destined for the inside of a double-quoted string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The escaped text, without surrounding quotes. |
Example
Escape an app name for a template placeholder in double quotes:
Source code in napt/powershell.py
strip_control_characters
Removes line breaks and other control characters from a value.
Quoting keeps a value safe inside a string, but an app name is also written into a comment line and a script filename, where a line break ends the comment (the remainder runs as code) or makes the filename invalid. Each run of control characters becomes a single space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw text, typically an app name from installer metadata or a recipe. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with every run of control characters replaced by a space |
str
|
and surrounding whitespace trimmed. |
Example
Clean a display name read from an installer manifest: