Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Get-Member give a full list of all properties and methods.

Tags:

powershell

I recently discovered that I can access a property for something that isn't listed when running it through get-member.

Here is an example, which I will demonstrate by using a custom psobject.

First, I'll create the hashtable (which I will later use to create the psobject):

$MyHashtable = @{'column1'="aaa";
                 'column2'="bbb";
                 'column3'="ccc"
}

Now I create my psobject:

PS C:\> $MyNewObject = New-Object -TypeName PSObject -Property $MyHashtable

Now if I run this new object through get-member, it shows:

PS C:\> $MyNewObject | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition                    
----        ----------   ----------                    
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()             
GetType     Method       type GetType()                
ToString    Method       string ToString()             
column1     NoteProperty System.String column1=aaa     
column2     NoteProperty System.String column2=bbb     
column3     NoteProperty System.String column3=ccc

So far, so good (although I suspect that a property called "PSObject" is missing from the above list, which I am about to demonstrate now).

However if I now do:

PS C:\> $MyNewObject.PSObject


Members             : {System.String column1=aaa, System.String column2=bbb, System.String column3=ccc, string ToString()...}
Properties          : {System.String column1=aaa, System.String column2=bbb, System.String column3=ccc}
Methods             : {string ToString(), bool Equals(System.Object obj), int GetHashCode(), type GetType()}
ImmediateBaseObject : 
BaseObject          : 
TypeNames           : {System.Management.Automation.PSCustomObject, System.Object}

This worked! How is that possible since "PSObject" wasn't listed as a property when we passed the object through Get-Member.

Hope someone can help.

like image 363
Sher Chowdhury Avatar asked Aug 23 '13 11:08

Sher Chowdhury


People also ask

What are some of the ways you can see the properties and methods available to a PowerShell command?

To display all the properties and methods available for the get-service cmdlet you need to pipeline Get-Member (alias gm). MemberType 'Property' is to display the specific property like machinename, servicename, etc.

What does get-member do in PowerShell?

The Get-Member command lets you list only members that are properties. There are several forms of properties. The cmdlet displays properties of any type if we set the MemberType parameter to the value Properties. The resulting list is still very long, but a bit more manageable: PowerShell Copy.

What command will list all things that make up a PowerShell object?

To discover information about an object (members), you can use the Get-Member cmdlet. The Get-Member cmdlet is a handy command that allows you find available properties, methods and so on for any object in PowerShell.

How do I get only certain types of member properties?

To get only certain types of members, such as NoteProperties , use the MemberType parameter. Adds the intrinsic members (PSBase, PSAdapted, PSObject, PSTypeNames) and the compiler-generated get_ and set_ methods to the display. By default, Get-Member gets these properties in all views other than Base and Adapted, but it does not display them.

How to get the list of the members of an object?

PowerShell provides Get-Member cmdlet to know the list of members of the objects. The Get-Member cmdlet. Get-Member cmdlet is used to get the members of the objects; the methods, properties, events etc, associated with the objects. It always need an input to get the members.

How to get only certain types of members of a class?

The Get-Member cmdlet gets the members, the properties and methods, of objects. To specify the object, use the InputObject parameter or pipe an object to Get-Member. To get information about static members, the members of the class, not of the instance, use the Static parameter. To get only certain types of members, such as NoteProperties, ...

Why does the get-member command not get all types of members?

Because the Get-Member part of the command does not have any parameters, it uses all of the default values. As such, it gets all member types, but it does not get static members and does not display intrinsic members.


1 Answers

Use Get-Member -Force to see all members

-Force [SwitchParameter] Adds the intrinsic members (PSBase, PSAdapted, PSObject, PSTypeNames) and the compiler-generated get_ and set_ methods to the display. By default, Get-Member gets these properties in all views other than "Base" and "Adapted," but it does not display them.

like image 147
malexander Avatar answered Sep 26 '22 12:09

malexander