PowerShell's Import-Clixml from a string

powershell, serialization, xml

Solution

Short Form

[Management.Automation.PSSerializer]::Deserialize($cliXmlString)

Long Form

Proof of Concept

@{ a=1; b = 2,3,4; c = 3.14 } |  Export-Clixml -Path ~\Test.clixml
$cliXmlString = ( Get-Content -Path ~\Test.clixml ) -join ''
[Management.Automation.PSSerializer]::Deserialize($cliXmlString)

Expected and Observed Output

Name                           Value
----                           -----
c                              3.14
a                              1
b                              {2, 3, 4}

Further Reading

PSSerializer.Deserialize(String) Method

Problem

Is there a way to run the `Import-Clixml` cmdlet on a string or XML object? It requires a file path as input to produce PowerShell objects and can't get input from an XML object. Since there is the ConvertTo-Xml cmdlet which serializes a PowerShell object into an XML object, why isn't there a convert from XML, which would do the opposite? I am aware of the `System.Xml.Serialization.XmlSerializer` class which would do just that. However, I would like to stick with cmdlets to do this. Is there a way to do this with cmdlets (probably just with `Import-Clixml`), without creating temporary files?

Original source