{"id":442,"date":"2011-07-07T10:02:47","date_gmt":"2011-07-07T16:02:47","guid":{"rendered":"http:\/\/jameskovacs.com\/2011\/07\/07\/token-replacement-in-powershell\/"},"modified":"2011-07-07T10:02:47","modified_gmt":"2011-07-07T16:02:47","slug":"token-replacement-in-powershell","status":"publish","type":"post","link":"https:\/\/www.jameskovacs.com\/index.php\/2011\/07\/07\/token-replacement-in-powershell\/","title":{"rendered":"Token Replacement in PowerShell"},"content":{"rendered":"<p>A question I often get asked is why <a href=\"http:\/\/github.com\/JamesKovacs\/psake\">psake<\/a> does not include something similar to NAnt\u2019s <a href=\"http:\/\/nant.sourceforge.net\/release\/latest\/help\/filters\/replacetokens.html\">&lt;replacetokens&gt;<\/a>. The reason is because it\u2019s so darn easy to do it in PowerShell. Given foo.txt.template:<\/p>\n<pre class=\"brush: plain;\">@@foo@@ is @@bar@@!!!<\/pre>\n<p>The following script will perform the replacement:<\/p>\n<pre class=\"brush: ps;\"># replace.tokens.ps1\n$foo = 'PowerShell'\n$bar = 'da bomb'\n(cat foo.txt.template) -replace '@@foo@@', &quot;$foo&quot; `\n                       -replace '@@bar@@', &quot;$bar&quot; `\n                       &gt; foo.txt<\/pre>\n<p>(Note the backticks (`) at the end of the line to denote continuation.)<\/p>\n<p>This script will produce:<\/p>\n<p><pre class=\"brush: plain;\">PowerShell is da bomb!!!<\/pre>\n<\/p>\n<p>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 &quot;$foo&quot;. This is PowerShell for performing variable replacements in strings. So &quot;$foo&quot; results in the word PowerShell whereas &#8216;$foo&#8217; results in the word $foo.<\/p>\n<p>So there you have it. Token replacement built right into PowerShell. Happy Scripting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A question I often get asked is why psake does not include something similar to NAnt\u2019s &lt;replacetokens&gt;. The reason is because it\u2019s 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 = &#8216;PowerShell&#8217; $bar = &#8216;da bomb&#8217; (cat foo.txt.template) -replace &#8216;@@foo@@&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-442","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":0,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}