Can anyone explain what appears to be odd behaviour when using Add-Member to add an attribute to a PSCustomObject object? For some reason, once you've added the member, the object is represented like a hashtable when displayed, even though it's still a PSCustomObject, e.g.:
Create a simple object:
[PSCustomObject] $test = New-Object -TypeName PSCustomObject -Property @{ a = 1; b = 2; c = 3; }
Check its type:
$test.GetType();
...which returns:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
Then get its contents:
$test;
...which returns:
c b a
- - -
3 2 1
Add a property:
Add-Member -InputObject $test -MemberType NoteProperty -Name d -Value 4 -TypeName Int32;
Confirm its type hasn't changed:
$test.GetType();
...which returns:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
Finally, get its contents again:
$test;
...which returns:
@{c=3; b=2; a=1; d=4}
Whereas I was hoping to get:
c d b a
- - - -
3 4 2 1
Any thoughts would be welcome, as I've been picking away at this for ages.
Many thanks