What happened inside the PowerShell ETS?

I want to find the PSObject.cs file in Github, but I cannot find it. And I haven’t find any file containing definition of this class (System.Management.Automation.PSObject). In source code of many other classes of PowerShell, I found that the PSObject class is used widely, but many properties or methods of the PSObject class are not documented in offical document of it.

The reason I want to find the source of PSObject is, in the following code, even the statements in the part 2 work correctly, but both the Name and the Age properties are not included in the output of the two statements in the part 3.

# part 1
$obj = [pscustomobject] @{Name = “Joe”; Age = 42}

# part 2
$obj.psextended.Name # “Joe”
$obj.Name # “Joe”
$obj.psextended.Age # 42
$obj.Age # 42

# part 3
$obj.GetType().GetMembers() | select Name, MemberType
$obj.psobject.GetType().GetMembers() | select Name, MemberType

The output of the first statement in part 3 is:

Name MemberType
---- ----------
ToString Method
GetType Method
Equals Method
GetHashCode Method

The output of the second statement in part 3 is:

Name MemberType
---- ----------
get_BaseObject Method
get_Members Method
get_Properties Method
get_Methods Method
get_ImmediateBaseObject Method
get_TypeNames Method
op_Implicit Method
op_Implicit Method
op_Implicit Method
op_Implicit Method
op_Implicit Method
AsPSObject Method
ToString Method
ToString Method
Copy Method
CompareTo Method
Equals Method
GetHashCode Method
GetObjectData Method
GetType Method
.ctor Constructor
.ctor Constructor
.ctor Constructor
BaseObject Property
Members Property
Properties Property
Methods Property
ImmediateBaseObject Property
TypeNames Property
AdaptedMemberSetName Field
ExtendedMemberSetName Field
BaseObjectMemberSetName Field

It seems that these two properties are not defined neither in $obj (a PSCustomObject) or its wrapper ($obj.psobject, a PSObject).

So, where the Name and the Age properties are defined? What happened when the statements in part 2 run? In other words, What happened inside the PowerShell ETS? Why there is not a “Name” property defined in $obj or $obj.psobject, but $obj.Name or $obj.psextended.Name works?

And, where can I find the real complete source code of the PSObject class?

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.

https://stackoverflow.com/questions/63772774/why-i-cannot-find-where-a-property-is-defined-in-powershell-where-is-the-source

Thanks

In the above code, you are getting members of the type, which is pscustomobject. Name and Age are not members of this type, but properties of an instance of PSCustomObject type.
you would do

$obj | Get-Member
$obj.PSObject.Properties

You can find the whole PowerShell source code at GitHub - PowerShell/PowerShell: PowerShell for every system! which is now for v 7.x

This post on StackOverflow is written by myself. I just copyed it to Powershell.org for asking more people.

@kvprasoon, Thank you for your reply. You said “In the above code, you are getting members of the type, which is pscustomobject. Name and Age are not members of this type, but properties of an instance of PSCustomObject type.” The instance is the $obj object in my code. As you said, the Name and Age properties are added to the $obj object, not added to the PSCustomObject type, but even this is true, call $obj.GetType() should reflect the definition of the “new type” that is the template of the $obj object, not the original PSCustomObject type. So, I’m still confused.

Can you give me some tips about the internal implementation of this? In other words, how properties are added dynamically to an object, and why the syntex $obj.property can work for these properties?