Persisting Objects with XML

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!


Posted Aug 21 2009, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.