title: Powershell: Force variable expansion
body: <p>How can I force string variable expansion?</p>
  
  <p>I need to read a string with one or more variable names in it (a template) and then expand it after I read the file. The key is that I must read the contents of the file before I declare the variables that will be used in the expansion. I've tried several ways but I can't get it to work.</p>
  
  <p>It's not an option to read the file after $environment is defined.</p>
  
  <p>Contents of name.txt: 
  $environment-RPT-INT</p>
  
  <pre><code>#example 1
  $name = gc "c:\temp\name.txt"
  $environment = "9065DEV" 
  $expanded = $ExecutionContext.InvokeCommand.ExpandString($name)
  $expanded
  
  #example 2
  $name = gc "c:\temp\name.txt"
  $environment = "9065DEV" 
  $expanded = $expanded = Invoke-Expression "`"$template`""
  $expanded
  
  #example 3
  $name = gc "c:\temp\name.txt"
  $environment = "9065DEV"
  $name = $name.Clone()
  $expanded = $ExecutionContext.InvokeCommand.ExpandString($name)
  $expanded
  </code></pre>
  
  <p>Any help is appreciated.</p>
  
  <p>Updated: Example 1 is now working for me. </p>