Loading a PowerShell hashtable from a file?

powershell

Solution

(I figured it out while putting together a repro)

PS> $content = ( Get-Content .\foo.pson | Out-String )
PS> $data = ( Invoke-Expression $content )

`Get-Content` returns an array with the lines in the file; the `Out-String` is used to join them together.

`Invoke-Expression` then runs the script, and the result is captured. This is open to injection attacks, but that's OK in my specific case.

Or, if you prefer your PowerShell terse:

PS> $data = gc .\foo.pson | Out-String | iex

(I can't find a shorter form of `Out-String`)

Problem

I've got a file containing some data in PowerShell Object Notation: ``` @{ X = 'x'; Y = 'y' } ``` I'd like to load this into a variable from the file.

Original source