Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Add-Member

Updated: October 17, 2013

Applies To: Windows PowerShell 4.0

Add-Member

Adds custom properties and methods to an instance of a Windows PowerShell object.

Syntax

Parameter Set: TypeNameSet
Add-Member -InputObject <PSObject> -TypeName <String> [-PassThru] [ <CommonParameters>]

Parameter Set: MemberSet
Add-Member [-MemberType] <PSMemberTypes> [-Name] <String> [[-Value] <Object> ] [[-SecondValue] <Object> ] -InputObject <PSObject> [-Force] [-PassThru] [-TypeName <String> ] [ <CommonParameters>]

Parameter Set: NotePropertyMultiMemberSet
Add-Member [-NotePropertyMembers] <IDictionary> -InputObject <PSObject> [-Force] [-PassThru] [-TypeName <String> ] [ <CommonParameters>]

Parameter Set: NotePropertySingleMemberSet
Add-Member [-NotePropertyName] <String> [-NotePropertyValue] <Object> -InputObject <PSObject> [-Force] [-PassThru] [-TypeName <String> ] [ <CommonParameters>]




Detailed Description

The Add-Member cmdlet lets you add members (properties and methods) to an instance of a Windows PowerShell object. For example, 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.

To use Add-Member, pipe the object to Add-Member, or use the InputObject parameter to specify the object. Use the MemberType parameter to specify the type of member that you want to add, use the Name parameter to assign a name to the new member, and use the Value parameter to set the value of the member.

The properties and methods that you add are added only to the particular instance of the object that you specify. Add-Member does not change the object type. To create a new object type, use the Add-Type cmdlet. You can also use the Export-Clixml cmdlet to save the instance of the object, including the additional members, in a file. Then you can use the Import-Clixml cmdlet to re-create the instance of the object from the information that is stored in the exported file.

Beginning in Windows PowerShell 3.0, Add-Member has new features that make it easier to add note properties to objects. You can use the NotePropertyName and NotePropertyValue parameters to define a note property or use the NotePropertyMembers parameter, which takes a hash table of note property names and values.

Also, beginning in Windows PowerShell 3.0, the PassThru parameter, which generates an output object, is needed less frequently. Add-Member now adds the new members directly to the input object of more types. For more information, see the PassThru parameter description.

Parameters

-Force

Adds a new member even the object has a custom member with the same name. You cannot use the Force parameter to replace a standard member of a type.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-InputObject<PSObject>

Specifies the object to which the new member is added. Enter a variable that contains the objects, or type a command or expression that gets the objects.


Aliases

none

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

True (ByValue)

Accept Wildcard Characters?

false

-MemberType<PSMemberTypes>

Specifies the type of the member to add. This parameter is mandatory.

The valid values for this parameter are: "NoteProperty,AliasProperty,ScriptProperty,CodeProperty,ScriptMethod,CodeMethod" AliasProperty, CodeMethod, CodeProperty, Noteproperty, ScriptMethod, and ScriptProperty.

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


Aliases

Type

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Name<String>

Specifies the name of the member to be added.


Aliases

none

Required?

true

Position?

2

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-PassThru

Returns the newly extended object. By default, this cmdlet does not generate any output.

For most objects, Add-Member adds the new members to the input object. However, when the input object is a string, Add-Member cannot add the member to the input object. For these objects, use the PassThru parameter to create an output object.

In Windows PowerShell 2.0, Add-Member added members only to the PSObject wrapper of objects, not to the object. Use the PassThru parameter to create an output object for any object that has a PSObject wrapper.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-SecondValue<Object>

Specifies optional additional information about AliasProperty, ScriptProperty, CodeProperty, or CodeMethod members. If used when adding an AliasProperty, this parameter must be a data type. A conversion (cast) to the specified data type is added to the value of the AliasProperty. For example, if you add an AliasProperty that provides an alternate name for a string property, you can also specify a SecondValue parameter of System.Int32 to indicate that the value of that string property should be converted to an integer when accessed by using the corresponding AliasProperty.

You can use the SecondValue parameter to specify an additional ScriptBlock when adding a ScriptProperty member. In that case, the first ScriptBlock, specified in the Value parameter, is used to get the value of a variable. The second ScriptBlock, specified in the SecondValue parameter, is used to set the value of a variable.


Aliases

none

Required?

false

Position?

4

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Value<Object>

Specifies the initial value of the added member. If you add an AliasProperty, CodeProperty, ScriptProperty or CodeMethod member, you can supply optional, additional information by using the SecondValue parameter.


Aliases

none

Required?

false

Position?

3

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-NotePropertyMembers<IDictionary>

Specifies a hash table or ordered dictionary of note property names and values. Type a hash table or dictionary in which the keys are note property names and the values are note property values.

For more information about hash tables and ordered dictionaries in Windows PowerShell, see about_Hash_Tables (http://go.microsoft.com/fwlink/?LinkID=135175).

This parameter is introduced in Windows PowerShell 3.0.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-NotePropertyName<String>

Adds a note property with the specified name.

Use this parameter with the NotePropertyValue parameter. The parameter name (-NotePropertyName) is optional.

This parameter is introduced in Windows PowerShell 3.0.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-NotePropertyValue<Object>

Adds a note property with the specified value.

Use this parameter with the NotePropertyName parameter. The parameter name (-NotePropertyValue) is optional.

This parameter is introduced in Windows PowerShell 3.0.


Aliases

none

Required?

true

Position?

2

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-TypeName<String>

Specifies a name for the type.

When the type is a class in the System namespace or a type that has a type accelerator, you can enter the short name of the type. Otherwise, the full type name is required. This parameter is effective only when the input object is a PSObject.

This parameter is introduced in Windows PowerShell 3.0.


Aliases

none

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • System.Management.Automation.PSObject

    You can pipe any object type to Add-Member.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • None or System.Object

    When you use the PassThru parameter, Add-Member returns the newly-extended object. Otherwise, this cmdlet does not generate any output.


Notes

  • You can add members only to PSObject objects. To determine whether an object is a PSObject 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 omit the parameter names, the unnamed parameter values must appear in this order: MemberType, Name, Value, SecondValue. If you include the parameter names, the parameters can appear in any order.

    You can use the $this automatic variable in script blocks that define the values of new properties and methods. The $this variable refers to the instance of the object to which the properties and methods are being added. For more information about the $this variable, see about_Automatic_Variables (http://go.microsoft.com/fwlink/?LinkID=113212).

Examples

-------------------------- EXAMPLE 1 --------------------------

These commands add the Status note property with a value of "Done" to the FileInfo object that represents the Test.txt file.

The first command uses the Get-ChildItem cmdlet (alias = "dir) to get the Test.txt file. It saves it in the $a variable.

The second and third commands add the note property to the object in $a. The third command omits the optional parameter names, so the parameter values must be in the correct Name-Value order. These commands are equivalent and can be used interchangeably.

The fourth command uses dot notation to get the value of the Status property of the object in $a. As the output shows, the value is "Done".


PS C:\> $a = dir c:\ps-test\test.txtPS C:\>$a | Add-Member -NotePropertyName Status -NotePropertyValue DonePS C:\>$a | Add-Member Status DonePS C:\>$a.StatusDone

-------------------------- EXAMPLE 2 --------------------------

These commands add the FileLength alias property to the object that represents the Test.txt file. The new property is an alias for the Length property.

The first command use the Get-ChildItem cmdlet (alias = "dir") to get the Test.txt file.

The second command adds the FileLength alias property.

The third command uses dot notation to get the value of the new FileLength property.


PS C:\> $a = dir c:\ps-test\test.txtPS C:\>$a | Add-Member -MemberType AliasProperty -Name FileLength -Value LengthPS C:\>$a.FileLength2394

-------------------------- EXAMPLE 3 --------------------------

These commands add the StringUse note property to a string. Because Add-Member cannot add types to String input objects, the command uses the PassThru parameter to generate an output object. The last command in the example displays the new property.

The command uses the NotePropertyMembers parameter, but omits the parameter name, which is optional. The value of the NotePropertyMembers parameter is a hash table. The key is the note property name, StringUse, and the value is the note property value, Display.


PS C:\> $a = "A string"PS C:\>$a = $a | Add-Member @{StringUse="Display"} -PassThruPS C:\>$a.StringUseDisplay

-------------------------- EXAMPLE 4 --------------------------

These commands add the PadBoth script method to a string object.

The first command creates a string and saves it in the $a variable.

The second command adds the Padboth script method to the object in the $a variable. The Value parameter defines the new script method. It uses the PadRight and PadLeft methods of a string to add one space the left and one space to the right of the string.

The Value parameter also uses the $this automatic variable, which represents the current object. The $this variable is valid only in script blocks that define new properties and methods.

The command includes the PassThru parameter which directs Add-Member to return an instance of the object that includes the new script property. By default, Add-Member adds members to PSObjects and does not generate any output.

The third command uses dot notation to call the new PadBoth script method on the object in the $a variable.


PS C:\> $a = "This is a string." PS C:\>$a = Add-Member -InputObject $a -MemberType ScriptMethod -Name PadBoth -Value {$p = $this.PadLeft($this.Length + 1); $p.PadRight($p.Length + 1)} -PassThruPS C:\>$a.Padboth()This is a string.

-------------------------- EXAMPLE 5 --------------------------

These commands add the "When" alias property to an event in the System event log. The event is an EventLogEntry object that is returned by the Get-EventLog cmdlet.

The "When" alias property is an alias for the TimeWritten property of the object. The SecondValue parameter is used to specify that the property value should be converted to type System.String when accessed by using the AliasProperty. The TimeWritten property is a DateTime object.

The first command uses the Get-EventLog cmdlet to get the newest event in the System event log. It stores the event in the $Event variable.

To demonstrate that the TimeWritten property is a DateTime type, the second command uses dot notation to get the TimeWritten property of that event and pipes it to the Get-Member cmdlet.

The third command uses the Add-Member cmdlet to add the When alias property to the object instance in the $Event variable. The Name parameter assigns the name, "When," and the Value parameter specifies that When is an alias for the TimeWritten property. The SecondValue parameter indicates that the value that the When method returns should be cast to a System.String type.

The fourth command uses dot notation to call the new When method. The command pipes the method value to the Get-Member cmdlet to confirm that it has returned a string.


PS C:\> $event = Get-EventLog -LogName System -Newest 1PS C:\>$event.TimeWritten | Get-MemberTypeName: System.DateTimeName                 MemberType     Definition----                 ----------     ----------Add                  Method         System.DateTime Add(System.TimeSpan value)AddDays              Method         System.DateTime AddDays(double value)AddHours             Method         System.DateTime AddHours(double value)AddMilliseconds      Method         System.DateTime AddMilliseconds(double value)AddMinutes           Method         System.DateTime AddMinutes(double value)...PS C:\>Add-Member -InputObject $event -MemberType AliasProperty -Name When -Value TimeWritten -SecondValue System.StringPS C:\>$event.When | Get-MemberTypeName: System.StringName             MemberType            Definition----             ----------            ----------Clone            Method                System.Object Clone()CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB)Contains         Method                bool Contains(string value)

-------------------------- EXAMPLE 6 --------------------------

This function copies all of the properties of one object to another object.

The first command in the function declares the function name and lists its parameters.

The Foreach loop uses the Get-Member cmdlet to get each of the properties of the From object. The commands within the ForEach loop are performed in series on each of the properties.

The Add-Member command adds the property of the From object to the To object as a NoteProperty. It uses the Force parameter add members with the same member name.

The last command in the function gives the new property the same name as the original property.


PS C:\> function Copy-Property ($From, $To){     foreach ($p in Get-Member -InputObject $From -MemberType Property)  {     Add-Member -InputObject $To -MemberType NoteProperty -Name $p.Name     -Value $From.$($p.Name) –Force     $To.$($p.Name) = $From.$($p.Name)  }}

Example 7

This example creates the Asset custom object.

The first command uses the New-Object cmdlet to create a PSObject. The command saves the PSObject in the $Asset variable.

The second command uses the [ordered] type accelerator to create an ordered dictionary of names and values. The command saves the result in the $d variable.

The third command uses the NotePropertyMembers parameter of the Add-Member cmdlet to add the dictionary in the $d variable to the PSObject. It uses the TypeName property to assign a new name, Asset, to the PSObject.

The fourth command sends the new Asset object in the $Asset variable to the Get-Member cmdlet. The output shows that the object has a type name of "Asset" and the note properties that we defined in the ordered dictionary.


PS C:\> $Asset = New-Object -TypeName PSObjectPS C:\>$d = [ordered]@{Name="Server30";System="Server Core";PSVersion="3.0"}PS C:\>$Asset | Add-Member -NotePropertyMembers $d -TypeName AssetPS C:\>$Asset | Get-Member   TypeName: AssetName        MemberType   Definition----        ----------   ----------Equals      Method       bool Equals(System.Object obj)GetHashCode Method       int GetHashCode()GetType     Method       type GetType()ToString    Method       string ToString()Name        NoteProperty System.String Name=Server30PSVersion   NoteProperty System.String PSVersion=3.0System      NoteProperty System.String System=Server Core

Related topics



Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.