Powershell Formatting for a String

The reason your current attempt doesn’t work is that single-quoted (‘) string literals in PowerShell are verbatim strings – no attempt will be made at expanding subexpression pipelines or variable expressions. If you want an expandable string literal without having to escape all the double-quotes (“) contained in the string itself, use a here-string: $mynumber … Read more

Using expanding strings as Powershell function parameters

In order to delay string interpolation and perform it on demand, with then-current values, you must use $ExecutionContext.InvokeCommand.ExpandString()[1] on a single-quoted string that acts as a template: function HelloWorld { Param ($Greeting, $Name) $ExecutionContext.InvokeCommand.ExpandString($Greeting) } HelloWorld ‘Hello, $Name!’ ‘World’ # -> ‘Hello, World!’ Note how ‘Hello, $Name!’ is single-quoted to prevent instant expansion (interpolation). Also … Read more