Persisting Objects with XML

by Aug 21, 2009

In the previous tip, you learned that Export/Import-CSV does not persist property types. Instead, all properties are converted to string. To preserve types as well, do not persist objects as CSV (because CSV has no place to store property types). Instead, use XML. It is just as easy. Here is an example:

Dir $env:windir | Select-Object Name, Length | Export-CliXML $home\test.xml
$result = Import-CliXML $home\test.xml
$result | Sort-Object Length

As you see, the re-loaded objects are sorted correctly (numerically).

Twitter This Tip! ReTweet this Tip!