Search code examples
powershellpowershell-4.0

Convert a stringified PSCustomObject back to a PSCustomObject


If I create a simple object like this

$myObject = [PSCustomObject]@{
Toto     = 'Kevin'
Language = 'Powershell'
State    = 'Texas'
ff       =  'kaka'}

and I convert it to string using expression like this

$stringObject = "$myObject"

I need to convert $stringObject back to PSCustomObject so i can access property like $object.Toto $object.State...

but I can't get it working using ConvertFrom-String.


Solution

  • [PSCustomObject]( Invoke-Expression ($stringObject -replace "\=", "='" -replace "\;", "';" -replace "\}", "'}"))
    

    is working for me