Why is [pscustomobject] the same as [psobject], even though a distinct [System.Management.Automation.PSCustomObject] type exists?
See original GitHub issueSurprisingly, [pscustomobject]
is the same as [psobject]
: both these type accelerators point to type [System.Management.Automation.PSObject]
, even though there is a distinct [System.Management.Automation.PSCustomObject]
type.
Mostly, this conflation goes unnoticed (and has come up before - see #2295), but:
-
what is the rationale for it?
-
it makes for surprising behavior on occasion - see below.
# Even though their names strongly suggest identity, they are different types.
> [pscustomobject] -eq [System.Management.Automation.PSCustomObject]
False
# Any object returned by a *command* (as opposed to an expression)
# returns $True for -is [psobject], and therefore also [pscustomobject]
> (Get-Item /) -is [pscustomobject]
True
# Casting anything other than a hashtable literal to [psobject] is a no-op,
# and therefore also with [pscustomobject]
> ([pscustomobject] 666).GetType().Name
Int32
Environment data
PowerShell Core v6.0.0-beta.4
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (9 by maintainers)
Top Results From Across the Web
about PSCustomObject - PowerShell
Even though you might think that [pscustomobject] should map to System.Management.Automation.PSCustomObject, the types are different.
Read more >powershell - Difference between PSObject, Hashtable, and ...
[PSCustomObject] is a type accelerator. It constructs a PSObject, but does so in a way that results in hash table keys becoming properties....
Read more >PowerShell Hash Table vs. PSCustomObject: Deep Dive & ...
While PowerShell concepts hash table and PSCustomObject may look similar, they are pretty different. Both have value, but which one you use ...
Read more >Filtering for Unique Objects in PowerShell
An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.
Read more >Ultimate Guide to Using PowerShell Add-Member Cmdlet
Or, you could include the -TypeName parameter and your object is no longer a PSCustomType object but a type named “MySuperObject”. What does...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Personally, I’ve always found this behavior extremely confusing:
For now, I suggest using
Documentation Needed
as a way to review issues later to determine where we need to add documentation (and not just for PRs where a doc change is also needed).Thanks for your continued depth of research into these advanced topics.