PowerShell Logo Small

Add-Member



This is the built-in help made by Microsoft for the command 'Add-Member', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Adds a user-defined custom member to an instance of a Windows PowerShell object.

SYNTAX


Add-Member [-MemberType] {AliasProperty | CodeProperty | Property | NoteProperty | ScriptProperty | Properties | PropertySet | Method | CodeMetho
d | ScriptMethod | Methods | ParameterizedProperty | MemberSet | Event | All} [-Name] <string> -InputObject <psobject> [[-Value] <Object>] [[-Sec
ondValue] <Object>] [-Force] [-PassThru] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Add-Member cmdlet adds a user-defined custom member to an instance of a Windows PowerShell object. It lets you add the following types of mem
bers: AliasProperty, CodeProperty, NoteProperty, ScriptProperty, PropertySet, CodeMethod, MemberSet, and ScriptMethod. You set the initial value
of the member by using the Value parameter. In the case of AliasProperty, ScriptProperty, CodeProperty, and CodeMethod, you can supply additional
information by using the SecondValue parameter.

The additional members are added to the particular instance of the object that you pipe to Add-Member or specify using the InputObject parameter.
The additional member is available only while that instance exists. You can use the Export-Clixml cmdlet to save the instance, including the add
itional members, to a file. The information stored in that file can be used by the Import-Clixml cmdlet to re-create the instance of the object.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113280
Get-Member
Export-Clixml
Import-Clixml

REMARKS

<

Examples


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

C:\PS>$a = (get-childitem)[0]

C:\PS> $a | add-member -membertype noteproperty -name Status -value done

C:\PS> $a | get-member -type noteproperty

TypeName: System.IO.DirectoryInfo

Name MemberType Definition
---- ---------- ----------
PSChildName NoteProperty System.String PSChildName=Co
PSDrive NoteProperty System.Management.Automation
PSIsContainer NoteProperty System.Boolean PSIsContainer
PSParentPath NoteProperty System.String PSParentPath=M
PSPath NoteProperty System.String PSPath=Microso
PSProvider NoteProperty System.Management.Automation
Status NoteProperty System.String Status=done



Description
-----------
These commands add the Status note property to a DirectoryInfo object returned by Get-ChildItem and assigns it a value of "done".

The first command gets the first object that Get-Childitem returns (index 0).

The second command adds the note property.

The third command uses a pipeline operator (|) to send the updated object to the Get-Member cmdlet. The output shows that the property has been a
dded.








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

C:\PS>$a = (get-childitem)[0]

C:\PS> $a | add-member -membertype aliasproperty -name FileLength -value Length

C:\PS> $a.filelength



Description
-----------
These commands add the FileLength alias property to a DirectoryInfo object returned by Get-ChildItem. The new property is an alias for the Length
property.

The first command gets the first object that Get-Childitem returns (index 0).

The second command adds the alias property.

The third command returns the value of the new FileLength property.








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

C:\PS>$a = "a string"

C:\PS> $a = $a | add-member -membertype noteproperty -name StringUse -value Display -passthru

C:\PS> $a.StringUse



Description
-----------
These commands add the StringUse a property to a string. Because the string is not a PSObject object, you must include the PassThru parameter in
the command to save the extended string in the variable. The last command in the example displays the new property.








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

C:\PS>$a = "this is a string"

C:\PS> $a = add-member -inputobject $a -membertype scriptmethod -name words `
-value {$this.split()} -passthru

C:\PS> $a.words()



Description
-----------
These commands add a script method to a string object. The script method exposes the Split() method of the System.String .NET Framework Class Lib
rary class to make it convenient to return the individual words in a string by calling a method named "Words" on the string object. Note that the
PassThru parameter is specified to force Add-Member to return the extended string object as output to be stored in the $a variable.








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

C:\PS>$event = get-eventlog -logname system -newest 1

C:\PS> $event.TimeWritten | get-member

C:\PS> add-member -inputobject $event -membertype aliasproperty -name When `
-value TimeWritten -secondvalue System.String

C:\PS> $event.When | get-member



Description
-----------
These commands add an AliasProperty to an EventLogEntry object returned by the Get-EventLog cmdlets. The AliasProperty is named "When" and 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 typ
e System.String when accessed by using the AliasProperty; the TimeWritten property is a DateTime object.

The first command uses the Get-EventLog cmdlet to retrieve the most recent event from the System event log and stores it in the $event variable.

The second command accesses the TimeWritten property of that event and pipes it to the Get-Member cmdlet to demonstrate that the property is a Da
teTime type. Add-Member is then used to add an AliasProperty member to the instance of the EventLogEntry object stored in the $event variable. Th
e Name parameter is used to set the name of the new member to "When" and the Value parameter is used to specify that it is an alias for the TimeW
ritten property. The SecondValue parameter is used to indicate that, when this new member is used, the value it returns should be cast from its o
riginal System.DateTime type to a System.String type.

The third command accesses the new member and pipes it to the Get-Member cmdlet to confirm that it is of type System.String.








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

C:\PS>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)
}
}



Description
-----------
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 to let the command ad
d members with the same member name.

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