title: Powershell Function Has Unexpected Return Value
body: <p>I have this Powershell code:</p>
  
  <pre><code>function getEmailHeaders
  {
      [System.Collections.Specialized.NameValueCollection]$headers = New-Object System.Collections.Specialized.NameValueCollection
      $headers.Add("name1", "VER=2011.2, NS=test.au, SEC=UNCLASSIFIED, ORIGIN=no-reply@test.bv.au")
      return $headers
  }
  </code></pre>
  
  <p>Then in my main method I have:</p>
  
  <pre><code>[System.Collections.Specialized.NameValueCollection]$headers = getEmailHeaders
  </code></pre>
  
  <p>However this throws the following exception:</p>
  
  <pre><code>System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert the "name1" value of type "System.String" to type "System.Collections.Specialized.NameValueCollection". ---&gt; System.Management.Automation.PSInvalidCastException
  : Cannot convert the "name1" value of type "System.String" to type "System.Collections.Specialized.NameValueCollection".
  </code></pre>
  
  <p>Why is $headers being read as a String rather than a NameValueCollection?</p>