Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member type in PowerShell (ScriptProperty, Property, and NoteProperty)

What are the member types in PowerShell, like ScriptProperty, Property, Method, NoteProperty, Alias, and EventProperty?

For example, CPU is ScriptProperty. How can we classify it as ScriptProperty?

I would like brief information about all of them.

like image 497
daniyalahmad Avatar asked Nov 14 '13 11:11

daniyalahmad


People also ask

What is NoteProperty in PowerShell?

NoteProperties are generic properties that are created by Powershell (as opposed to properties that are inherited from a specific dotnet object type).

Which member type adds property whose value is calculated by a script in PowerShell?

The Add-Member cmdlet lets you add members (properties and methods) to an instance of a PowerShell object. For instance, you can add a NoteProperty member that contains a description of the object or a ScriptMethod member that runs a script to change the object.

What is script property in PowerShell?

A script property defines a property whose value is the output of a script. In the following example, the VersionInfo property is added to the System. IO. FileInfo type.

What is get-member in PowerShell?

Description. 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.


2 Answers

There used to be a great introduction on MSDN to the PowerShell Extended Type System (unfortunately lost with the changes since PSH v1).

Essentially PowerShell allows an underlying .NET object to be wrapped with additional members via the PSObject type. This can be done in a number of ways:

  • Using Add-Member (giving maximum control)
  • Specifying additional properties by passing a hash rather than a name to Select-Object's property parameter
  • Using New-Object to create a PSObject and passing
  • In .NET code (C#, VB, …) using the underlying PSObject properties and PSMemberInfo sub-types.

The different types of "extended" member are represented by those PSMemberInfo sub-types, including:

  • NoteProperty: a .NET object or value.
  • AliasProperty: an alias for another property (eg. a collection could have both a Count and a Length property with one being another name for the other).
  • ScriptProperty: a property with get and set methods written in PowerShell.
  • CodeProperty: a property with get and set methods written in C#, VB, ….

and so forth.

like image 193
Richard Avatar answered Sep 20 '22 16:09

Richard


See the PSMemberTypes Enumeration page

like image 39
Shay Levy Avatar answered Sep 22 '22 16:09

Shay Levy