Which object types can be added via script with Add-Member

by willsteele at 2012-08-15 12:39:13

In reading through the Get-Help for Add-Member I see multiple types of member types which can be added, but, only find practical implementations of a few of them. When properties can be used with an advanced script? And, conversely, which cannot? For reference, I get the following from Get-Help and have emphasized the member types:

Get-Help Add-Member -Parameter MemberType

-MemberType <PSMemberTypes>
Specifies the type of the member to add. This parameter is mandatory.
The valid values for this parameter are: [b][i]“NoteProperty,AliasProperty,ScriptProperty,CodeProperty,ScriptMethod,CodeMethod” AliasProperty, CodeMethod, CodeProperty, Noteproperty, ScriptMethod, and ScriptProperty.[/i][/b]
For information about these values, see “PSMemberTypes Enumeration” in MSDN at http://msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.psmembertypes(v=vs.85).aspxhttp://msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.psmembertypes(v=vs.85).aspx.
Not all objects have every type of member. If you specify a member type that the object does not have, Windows PowerShell returns an error.

Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? true
by poshoholic at 2012-08-15 13:00:15
All types can be added with Add-Member. I’ve actually written script at one point or another for each of these types. Here is that list sorted by frequency that they are used in practice when scripting based on my experience with them, plus with some explanations to help understand how they can be applied to custom object definition:

NoteProperty: A general purpose property; can be read-only when created from read-only variables in New-Module -AsCustomObject calls.
ScriptProperty: A property that is calculated at the time that it is rendered/displayed in PowerShell; the value does not actually exist on the object, it is only shown at runtime; useful when defining dynamic properties, read-only properties, or read-write properties, especially when the property is determined by referencing a property or member on the object itself
ScriptMethod: A method that can reference the object itself via the $this variable; does not necessarily have to reference the object at all though; this is the easiest way to define methods for custom objects
AliasProperty : An alias for another property; useful when you rename a property at some point and want to maintain backwards compatibility with the old name
CodeProperty: Like ScriptProperty, this can create properties that have just getters (read-only) or both getters and setters (read-write); this property references static properties defined on the object type itself (static properties are defined in compiled C# code, hence the “Code” prefix)
CodeMethod: A method that references a static method on the object itself (static methods are defined in compiled C# code, hence the “Code” prefix)

There aren’t many use cases that I have come across for the last two though. I use CodeProperty heavily in my wmix module. Other than that I have only used those two sparingly from time to time.
by willsteele at 2012-08-15 14:38:39
Thanks for the breakdown and the ordering. I think when I tried to play with code samples I picked both code* members types, so, seeing how they fit in the bigger picture, puts it back into perspective. Much appreciated, Will