title: How do I dynamically set the type of PowerShell variables?
body: <p>Is it possible to get a System.Type from a string and then apply it to a variable?</p>
  
  <p>The following is the function that I tried to convert to make it safer (i.e. not involve invoking a string):</p>
  
  <pre><code>Function ParseLine($Type, $VariableName, $Value){
      Invoke-Expression "[$Type] `$$VariableName = $Value"
  }
  </code></pre>
  
  <p>I looked at <code>New-Variable</code> and <code>Set-Variable</code>, but there is no type-setting-related parameter in the definition.</p>
  
  <p>I expected something that looks like below, but I couldn't find the parameter <code>Type</code> or equivalent:</p>
  
  <pre><code>Function ParseLine($Type, $VariableName, $Value){
      New-Variable -Name $VariableName -Value $Value -Type ([type] $Type)
  }
  </code></pre>
  
  <p><strong>Context:</strong> I am experimenting to create a simple parser definition that looks like:</p>
  
  <pre><code>$ResponseLogFormat = New-InputFormat {
      ParseLine -Type int -VariableName RecordLength
      RepeatedSection -RepeatCount $RecordLength {
          ParseLine string +ComputerName
          ParseLine double +AverageResponse
      }
  }
  
  $ResponseLogFormat.Parse( $FilePath )
  </code></pre>