[Array] becomes [ArrayList] when imported via Import-CliXml

Serialising and Deserialising data structures is pretty easy when using Export-CliXml/Import-CliXml. However, there are some gotchas. In addition to the already described bug when using [ordered] hashtable in PowerShell v3 with Import-CliXml there is another error when importing [Array] with Import-CliXml: an [Array] becomes an [ArrayList]. The following code shows the problem:

PS > $ht = @{};
PS > $ht.Array = @();
PS > $ht.Array.GetType();
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS > $ht | Export-Clixml ht.xml;
PS > $ht2 = Import-Clixml ht.xml;
PS > $ht2.Array.GetType();
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object

Again, this error seems to occur when you do the actual import, because the underlying xml file still contains the correct type information:




The problem is that these data types behave differently. For example, check the “Length” property. It returns the number of entries (Count) on [Array] and the individual length of the the list items on [ArrayList]:

PS > $a = @("11", "22");
PS > $a.Length;

$al = New-Object System.Collections.ArrayList($null);
PS > $al.Add("11");
PS > $al.Add("22");
PS > $al.Length;

I filed a bug on Microsoft Connect under [Array] becomes [ArrayList] when imported via Import-CliXml.

By the way: it does not make a difference if you use “Set-StrictMode -Version 3” or not.


  1. […] unfortunately, there seem to be some subtle bugs in the implementation of the Cmdlet (remember the implicit type conversion I wrote about […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: