title: Find variables within a string in PowerShell
body: <p>I've a sample code like this:</p>
  
  <pre><code>[string] $Text = "This is a string text with some $($variable.Option) and $otherOption and that's it". 
  </code></pre>
  
  <p>Now what I would like to know is it possible to split that <code>$Text</code> into standard strings and variables separately? So that when I pass this <code>$Text</code> variable to a method it's able to extract <code>$($variable.Option)</code> separately? </p>
  
  <p>I know it's a long shot but maybe it's not being processed right away at assignment time?</p>
  
  <p>The ultimate goal is to create a <a href="http://evotec.pl/powershell-how-to-format-powershell-write-host-with-multiple-colors/" rel="nofollow">better version of method I wrote</a> for making colorful powershell:</p>
  
  <pre><code>function Write-Color([String[]]$Text, [ConsoleColor[]]$Color = "White", [int]$StartTab = 0, [int] $LinesBefore = 0,[int] $LinesAfter = 0) {
      $DefaultColor = $Color[0]
      if ($LinesBefore -ne 0) {  for ($i = 0; $i -lt $LinesBefore; $i++) { Write-Host "`n" -NoNewline } } # Add empty line before
      if ($StartTab -ne 0) {  for ($i = 0; $i -lt $StartTab; $i++) { Write-Host "`t" -NoNewLine } }  # Add TABS before text
      if ($Color.Count -ge $Text.Count) {
          for ($i = 0; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine } 
      } else {
          for ($i = 0; $i -lt $Color.Length ; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine }
          for ($i = $Color.Length; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $DefaultColor -NoNewLine }
      }
      Write-Host
      if ($LinesAfter -ne 0) {  for ($i = 0; $i -lt $LinesAfter; $i++) { Write-Host "`n" } }  # Add empty line after
  }
  </code></pre>
  
  <p>Usually I can assign colors by doing something like </p>
  
  <pre><code>write-color -Text "[View][$($singleView.Title)]", 
                    "[Row Limit: $($singleView.RowLimit)]",
                    "[Paged: $($singleView.Paged)]",
                    "[Default View: $($singleView.DefaultView)]",
                    "[Style ID: $($singleView.StyleID)]" -Color Yellow, Green, Red, Gray, Green
  </code></pre>
  
  <p>But this means I get colors for the whole "line". If i would like to get normal text colors in one color and variables being in second color I would have to do something like this:</p>
  
  <pre><code>write-color -Text "[View: ", "$($singleView.Title)", "]",
                    "[Row Limit: ", "$($singleView.RowLimit)", "]" `
              -Color Yellow, Green, Yellow, Yellow, Green, Yellow
  </code></pre>
  
  <p>It's not bad.. but I just thought if this can be accomplished in a better way where simple text is one color and variables are the 2nd. If I would want to go even further and have $true being in Green and False being in Red that would also require some parsing.     </p>