title: PowerShell - InvalidCastException when returning Boolean to explicitly declared variable
body: <p>I've written a PowerShell script to perform some pre-installation setup for a series of patches I'm deploying to client computers across our estate and I'm hitting a bit of an odd issue that I can't wrap my head around.</p>
  
  <p>The setup patch checks the 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config' file due to a "feature" of PowerShell 2.0 whereby the application uses .NET Framework 2.0.0 by default instead of 4.5.2, preventing certain functions from being executed. If the file doesn't exist or the evaluated values don't match a specification, I add the XML file and provide the necessary values.</p>
  
  <p>The command I run is as follows:</p>
  
  <pre><code>$psConfigDir = "C:\Windows\System32\WindowsPowerShell\v1.0"
  $psConfigFileName = "powershell.exe.config"
  
  [boolean]$psExeXml = Set-PSRuntimeConfigs -FilePath ( [String]::Format("{0}\{1}", $psConfigDir, $psConfigFileName) ) -CLRVersions @("v4.0.30319", "v2.0.50727")
  </code></pre>
  
  <p>...and the <code>Set-PSRuntimeConfigs</code> method is found in a PowerShell Module I created with the code below:</p>
  
  <pre><code>Function Set-PSRuntimeConfigs {
      [CmdletBinding()]
      Param(
              [String]$FilePath,
              [System.Collections.ArrayList]$CLRVersions
           )
  
      Try {
          $xmlWriter = New-Object System.Xml.XmlTextWriter($FilePath, $null)
          $xmlWriter.Formatting = "Indented"
          $xmlWriter.Indentation = 4
  
          $xmlWriter.WriteStartDocument()
          $xmlWriter.WriteStartElement("configuration")
  
          $xmlWriter.WriteStartElement("startup")
          $xmlWriter.WriteAttributeString("useLegacyV2RuntimeActivationPolicy", $true)
  
          $CLRVersions | ForEach-Object {
              $xmlWriter.WriteStartElement("supportedRuntime")
              $xmlWriter.WriteAttributeString("version", $_)
              $xmlWriter.WriteEndElement()
          }
  
          $xmlWriter.WriteEndElement()
          $xmlWriter.WriteEndElement()
          $xmlWriter.WriteEndDocument()
  
          $xmlWriter.Close()
          $xmlWriter.Dispose()
  
          return $true
      } Catch {
          echo "ERROR: Exception occurred during XML write process!"
          echo "ERROR: Exception message: $($_.Exception.Message)"
          return $false
      }
  }
  </code></pre>
  
  <p>However, the function is returning an InvalidCastException when trying to assign the result of the function to the <code>$psExeXml</code> variable. Oddly, PowerShell returns with an error stating that [System.Object()] cannot be converted to type [Boolean] despite the fact that only <code>$true</code> or <code>$false</code> is returned from the function.</p>
  
  <p>My first thought is that an exception was being thrown by the function due to a code issue but the function is written to report the error in the prompt and just return <code>$false</code> in that case... Regardless, I'm stuck and can't figure out where to proceed with this...</p>