Search code examples
powershellserializationhashmaphashtablepickle

Does PowerShell support HashTable Serialization?


If I want to write an object / HashTable to disk and load it up again later does PowerShell support that?


Solution

  • Sure, you can use PowerShell's native CliXml format:

    @{
      a = 1
      b = [pscustomobject]@{
        prop = "value"
      }
    } | Export-Clixml -Path hashtable.ps1xml
    

    Deserialize with Import-CliXml:

    PS C:\> $ht = Import-CliXml hashtable.ps1xml
    PS C:\> $ht['b'].prop -eq 'value'
    True