Quick template text file implementation in Powershell

 

If you write scripts, there are good chances you have some of them whose sole purpose is to duplicate configuration files or reports with data depending on variables.
A quick way to implement this in Powershell is to use the combined power of Here-Strings and expressions using variables.

The basic idea is to store the template as a text file, using variables and derived expressions to act as data placeholders :

<pre class="lang:default decode:true ">This is my template

I put data from simple variable content : $name, $first
I put data from expression to timestamp the output : $(Get-Date)
And I even add calculations if needed $($mb/1MB) MB 

Then we’ll have to figure out how to read that template file, let Powershell replace the variables and expressions by their contents, and do whatever we need.

If you simply read the contents of the file with Get-Content, the variables are not expanded, so you’ll have to figure out how to build an instruction, expression, or scriptblock that will make Powershell interpret it.

Since we are talking about expressions, let’s use Invoke-Expression. The only missing piece of the puzzle is how to transform that string (or array of strings) we read with Get-Content into a valid Powershell expression.

Here (!) come the Here-Strings by noticing that the following code will build a valid here-string from any variable

<pre class="lang:ps decode:true " title="Here-String from any variable">"@`"`r`n$variable`r`n`"@"

If you have trouble reading that expression, remember that

  • the opening and ending tags of a here-string must be on their own line, hence the CR LF pairs;
  • the CR LF, and double-quote symbol must be written using the backtick as we are inside double-quoted string.

Then our full solution will be:

<pre class="lang:default decode:true " title="Text Templates in Powershell">$template = Get-Content 'mytemplate.txt' -Raw
$expanded = Invoke-Expression "@`"`r`n$template`r`n`"@"