Casting in PowerShell. Weird syntax

casting, powershell, powershell-2.0

Solution

$x = [xml](Get-Content myxml.xml)

Problem

Reading XML from a file into a variable can be done like this: ``` [xml]$x = Get-Content myxml.xml ``` But why not: ``` $x = [xml]Get-Content myxml.xml ``` Which gives: ``` Unexpected token 'Get-Content' in expression or statement. At line:1 char:20 + $x=[xml]get-content <<<< myxml.xml + CategoryInfo : ParserError: (get-content:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken ``` That is, why is the cast operation done on the left-hand side of the equals sign? Typically in programming languages the casting is done on the right-hand side like (say) in Java: ``` a = (String)myobject; ```

Original source