PowerShell type accelerators: PSObject vs PSCustomObject

Viewed 7156

In PowerShell v3.0 PSCustomObject was introduced. It's like PSObject, but better. Among other improvements (e.g. property order being preserved), creating object from hashtable is simplified:

[PSCustomObject]@{one=1; two=2;}

Now it seems obvious that this statement:

[System.Management.Automation.PSCustomObject]@{one=1; two=2;}

would work the same way, because PSCustomObject is an "alias" for full namespace + class name. Instead I get an error:

Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Management.Automation.PSCustomObject".

I listed accelerators for both types of objects:

[accelerators]::get.GetEnumerator() | where key -Like ps*object

    Key            Value
    ---            -----
    psobject       System.Management.Automation.PSObject
    pscustomobject System.Management.Automation.PSObject

and discovered that both reference the same PSObject class - this has to mean that using accelerators can do a bunch of other stuff than just making the code shorter.

My questions regarding this issue are:

  1. Do you have some interesting examaples of differences between using an accelerator vs using full type name?
  2. Should using full type name be avoided whenever an accelerator is available as a general best practice?
  3. How to check, maybe using reflection, if an accelerator does other stuff than just pointing to underlying class?
2 Answers
Related