A question I often get asked is why psake does not include something similar to NAnt’s <replacetokens>. The reason is because it’s so darn easy to do it in PowerShell. Given foo.txt.template:
@@foo@@ is @@bar@@!!!
The following script will perform the replacement:
# replace.tokens.ps1 $foo = 'PowerShell' $bar = 'da bomb' (cat foo.txt.template) -replace '@@foo@@', "$foo" ` -replace '@@bar@@', "$bar" ` > foo.txt
(Note the backticks (`) at the end of the line to denote continuation.)
This script will produce:
PowerShell is da bomb!!!
You could easily write a function that would take care of the nitty gritty details. The @@var@@ is arbitrary. You could use any sequence you like. You can even perform regex matches in the @@var@@ expression if needed. Note the double quotes around "$foo". This is PowerShell for performing variable replacements in strings. So "$foo" results in the word PowerShell whereas ‘$foo’ results in the word $foo.
So there you have it. Token replacement built right into PowerShell. Happy Scripting!