Contact Info

Crumbtrail » Administration » Powershell » Powershell 1.0 » Add-Member

Add-Member - Powershell 1.0 CmdLet

Microsoft Windows PowerShell is a command-line shell and scripting tool based on the Microsoft .NET Framework. It is designed for system administrators, engineers and developers to control and automate the administration of Windows and applications.

More than hundred command-line tools (so called "cmdlets") can be used to perform system administration tasks and Windows Management Instrumentation (WMI). These cmdlets are easy to use, with standard naming conventions and common parameters, and standard tools for piping, sorting, filtering, and formatting data and objects.


Add a member to an instance of a PowerShell object


-memberType PSMemberType
        The type of the member to add. Valid values for this are:
        {AliasProperty | CodeProperty| Property | NoteProperty | ScriptProperty
       | Properties | PropertySet | Method | CodeMethod | ScriptMethod | Methods
       | ParameterizedProperty | MemberSet | All}

   -inputObject psobject
        The object to which the new member is added. (Required)
        Enter a variable that contains the objects or type a command or 
        expression that gets the objects.

   -name string
        The name of the member to be added.

   -value Object
        The initial value of the added member. If you add an AliasProperty,
        CodeProperty or CodeMethod member, you can supply optional, addi
        tional information by using the -SecondValue parameter.

   -force SwitchParameter
        Adds a new member even if one with the same name already exists. Does not
        work for core members of a type.

   -passThru SwitchParameter
        Pass the newly-extended object created by this cmdlet along the pipeline.

        Supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable.

Add a note property to a DirectoryInfo object returned by Get -ChildItem.

PS C:>$a = (get-childitem)[0]
$a | add-member -membertype noteproperty -name MyNote -value SomeSampleText
$a | get-member

The above names a new property MyNote and assigns it the value: SomeSampleText.
Piping the updated object to Get-Member demonstrates that the property has been added.
For more examples run get-help Add-Member -detailed