Add-Member

Add a user-defined custom member to a PowerShell object instance.

Syntax
      Add-Member [-memberType PSMemberType] 
	      [-name] string 
           [[-value] Object] [[-secondValue] Object]
               -inputObject psobject 
                 [-force] [-passThru] [CommonParameters]

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

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

Notes:
You can only add members to PSObjects. To determine if a object is a PS Object, use the 'is' operator.
For example, to test an object stored in the $obj variable, type "$obj -is [PSObject]".

The names of the MemberType, Name, Value, and SecondValue parameters are optional. If you include the parameter names, the parameters can appear in any order. If you omit the parameter names, the unnamed parameter values must appear in this order: MemberType, Name, Value, SecondValue.

When specifying multiple values for a parameter, use commas to separate the values: "parameter-name value1, value2".

Example

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

"What we think, we become" - Siddhartha Gautama (Buddha)

Related Powershell Commands:

Get-Member
Export-CLIXML
Import-CLIXML



Back to the Top

Simon Sheppard
SS64.com