question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why is [pscustomobject] the same as [psobject], even though a distinct [System.Management.Automation.PSCustomObject] type exists?

See original GitHub issue

Surprisingly, [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:closed
  • Created 6 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
AikenBMcommented, Mar 7, 2018

Personally, I’ve always found this behavior extremely confusing:

PS> (New-Object -TypeName System.Management.Automation.PSObject -Property @{Property = 'Value'}).GetType().FullName
System.Management.Automation.PSCustomObject

PS> (New-Object -TypeName System.Management.Automation.PSCustomObject -Property @{Property = 'Value'}).GetType().FullName
New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.Management.Automation.PSCustomObject.

PS> ([PSCustomObject]@{Property = 'Value'}).GetType().FullName
System.Management.Automation.PSCustomObject

PS> ([PSObject]@{Property = 'Value'}).GetType().FullName
System.Collections.Hashtable
2reactions
SteveL-MSFTcommented, Jul 26, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found