How to export properties to a text file via a template

The first step is to create a template, the format of the template is quite flexible. In this example the following html template is used:

<html>
<table border="1">
<tr>
<td>{Author}</td>
<td>{Classification}</td>
<td>{Created By}</td>
</tr>
</table>
</html>
 

The following script will then replace the property names with their corresponding values:

$templatePath = "C:\temp\template.txt"
$destinationPath = "C:\temp\template.html"

$regex = [regex]"\{+(?i)\b[A-Z\s]+\b\}"
$template = Get-Content -Path $templatePath
$propertyMatches = $regex.Matches($template)
foreach($propertyMatch in $propertyMatches){
$propertyName = $propertyMatch.Value.Trim( @('{','}') )
$propertyValue = $file.$propertyName
$template = ($template -replace $propertyMatch.Value, $propertyValue)
}
Write-Host $template
$template | Out-File $destinationPath -Append
  • Define the template file that should be used 
  • Define the destination file that should be created / appended to
  • Match the regex against the content of the template file to find the required properties
  • Replace all the property names in the template file with their actual values
  • Write the result to the destination file

Example

Using the template shown before the output should look similar to this:

<html>
<table border="1">
<tr>
<td>Assembly1.dwg</td>
<td>Design Document</td>
<td>Administrator</td>
</tr>
</table>
</html>

Remarks

  • This code snippet will always use the same destination file and append to it.