Skip to content

[System.Management.Automation.PSCustomObject] weirdness #2295

Closed

Description

@vors

Steps to reproduce

There are few issues with casting to [System.Management.Automation.PSCustomObject]. Note the full name! The short name [PSCustomObject] works just fine, I guess it uses a different (type accelerator) code path.

First part

Try to create a [PSCustomObject] as described in a numerous of blog posts (example)

> $a = [PSCustomObject]@{a = 'a'; b = 'b'}       
> $a      
a b                                                                                                                                         
- -                                                                                                                     
a b 

> $a.GetType().FullName            
System.Management.Automation.PSCustomObject                                                         

So far so good.
Now, try to do it with the full name

> [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type                                       
"System.Management.Automation.PSCustomObject".
At line:1 char:2
+  [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException

Expected behavior

Conversion succeed.

Actual behavior

> [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type                                       
"System.Management.Automation.PSCustomObject".
At line:1 char:2
+  [System.Management.Automation.PSCustomObject]@{a = 'a'; b = 'b'}
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException

Second part

Now try to cast the original object $a

> [System.Management.Automation.PSCustomObject]$a                    
Cannot convert the "@{a=a; b=b}" value of type "System.Management.Automation.PSCustomObject" to type 
"System.Management.Automation.PSCustomObject".
At line:1 char:2
+  [System.Management.Automation.PSCustomObject]$a
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException

The error message is pretty weird, since no actual conversion should be required.

Note: this affects UX, because auto-completion on [pscu will expend it into [System.Management.Automation.PSCustomObject and not [PSCustomObject.
This is also inconsistent with [pso that expends into [psobject and not [System.Management.Automation.PSObject]

Also, note that this works fine

> [System.Management.Automation.PSObject]@{a = 'a'; b = 'b'}

Environment data

> $PSVersionTable
Name                           Value                                                                                                        
----                           -----                                                                                                        
PSVersion                      6.0.0-alpha                                                                                                  
PSEdition                      Core                                                                                                         
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                      
BuildVersion                   3.0.0.0                                                                                                      
GitCommitId                    v6.0.0-alpha.10                                                                                              
CLRVersion                                                                                                                                  
WSManStackVersion              3.0                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                          
SerializationVersion           1.1.0.1 

cc Jeffrey Snover (@jpsnover)

Activity

  1. added
    Issue-Bug
    WG-Engine
    WG-Language
    on Sep 17, 2016
  2. changed the title [-][System.Management.Automation.PSCustomObject] weiredness[/-] [+][System.Management.Automation.PSCustomObject] weirdness[/+] on Sep 17, 2016
  3. vors commented on Sep 20, 2016

    @vors
    CollaboratorAuthor

    James Truher (@JamesWTruher) found a part that may explain some:

    > [pscustomobject].FullName
    System.Management.Automation.PSObject
    

    So looks like both [pscustomobject] and [psobject] type accelerators point to the same PSObject type.

  4. added
    Up-for-Grabs
    on Nov 2, 2016
  5. added a commit that references this issue on Jan 9, 2017
    57d5a10
  6. added a commit that references this issue on Aug 6, 2026
    1baccc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugResolution-FixedUp-for-GrabsWG-EngineWG-Language

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions