CmdID

CmdID / PowerShell / Cmdlets / Add

Add-Member

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

From the PowerShell reference 11 parameters

Opens CmdID with your command, read as PowerShell. It is read in your browser and sent nowhere.

What it does

from the PowerShell reference, CC BY 4.0

Adds custom properties and methods to an instance of a PowerShell object. The Add-Member cmdlet lets you add members (properties and methods) to an instance of a PowerShell object. For instance, 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. The MemberType parameter indicates the type of member that you want to add. The Name parameter assigns a name to the new member, and the Value parameter sets 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 doesn't 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's 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.

Add-Member -InputObject <PSObject> -TypeName <String> [-PassThru] [<CommonParameters>]
Add-Member -InputObject <PSObject> [-TypeName <String>] [-Force] [-PassThru] [-NotePropertyMembers] <IDictionary> [<CommonParameters>]
Add-Member -InputObject <PSObject> [-TypeName <String>] [-Force] [-PassThru] [-NotePropertyName] <String> [-NotePropertyValue] <Object> [<CommonParameters>]
Add-Member -InputObject <PSObject> [-MemberType] <PSMemberTypes> [-Name] <String> [[-Value] <Object>] [[-SecondValue] <Object>] [-TypeName <String>] [-Force] [-PassThru] [<CommonParameters>]

Add a note property to a PSObject

$A = Get-ChildItem C:\ps-test\test.txt $A | Add-Member -NotePropertyName Status -NotePropertyValue Done $A.StatusIdentify

Add an alias property to a PSObject

$A = Get-ChildItem C:\Temp\test.txt $A | Add-Member -MemberType AliasProperty -Name Size -Value Length $A.SizeIdentify

Add a StringUse note property to a string

$A = "A string" $A = $A | Add-Member -NotePropertyMembers @{StringUse="Display"} -PassThru $A.StringUseIdentify

Add a script method to a FileInfo object

$A = Get-ChildItem C:\Temp\test.txt $S = {[Math]::Round(($this.Length / 1MB), 2)} $A | Add-Member -MemberType ScriptMethod -Name "SizeInMB" -Value $S $A.SizeInMB()Identify

Create a custom object

$Asset = New-Object -TypeName psobject $Asset | Add-Member -NotePropertyMembers @{Name="Server30"} -TypeName Asset $Asset | Add-Member -NotePropertyMembers @{System="Server Core"} $Asset | Add-Member -NotePropertyMembers @{PSVersion="4.0"} $Asset | Get-Member -MemberType PropertiesIdentify

Add an AliasProperty to an object

$user = [pscustomobject]@{ Name = 'Doris' Age = '20' } $addMemberSplat = @{ MemberType = 'AliasProperty' Name = 'IntAge' Value = 'Age' SecondValue = 'UInt32' } $user | Add-Member @addMemberSplat $user | Get-MemberIdentify

Add get and set methods to a custom object

$user = [pscustomobject]@{ Name = 'User1' Age = 29 StartDate = [datetime]'2019-05-05' Position = [pscustomobject]@{ DepartmentName = 'IT' Role = 'Manager' } } $addMemberSplat = @{ MemberType = 'ScriptProperty' Name = 'Title' Value = { $this.Position.Role } # getter SecondValue = { $this.Position.Role = $args[0] } # setter } $user | Add-Member @addMemberSplat $user | Get-MemberIdentify

Its page in the reference: Add-Member.

Parameters

11 in the PowerShell reference
-ForceBy default, Add-Member can't add a new member if the object already has a member with the same. When you use the Force parameter, Add-Member replaces the existing member with the new member. You can't use the Force parameter to replace a standard member of a type.
-InputObjectSpecifies 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.
-MemberTypeSpecifies the type of the member to add. This parameter is required. The acceptable values for this parameter are: - AliasProperty - CodeMethod - CodeProperty - NoteProperty - ScriptMethod - ScriptProperty For information about these values, see PSMemberTypes Enumeration in the PowerShell SDK. Not all objects have every type of member. If you specify a member type that the object doesn't have, PowerShell returns an error.
-NameSpecifies the name of the member that this cmdlet adds.
-NotePropertyMembersSpecifies a hashtable or ordered dictionary that contains key-value pair representing NoteProperty names and their values. For more information about hash tables and ordered dictionaries in PowerShell, see about_Hash_Tables. This parameter was introduced in Windows PowerShell 3.0.
-NotePropertyNameSpecifies the note property name. Use this parameter with the NotePropertyValue parameter. This parameter is optional. This parameter was introduced in Windows PowerShell 3.0.
-NotePropertyValueSpecifies the note property value. Use this parameter with the NotePropertyName parameter. This parameter is optional. This parameter was introduced in Windows PowerShell 3.0.
-PassThruReturns an object representing the item with which you are working. By default, this cmdlet doesn't 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 can't 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.
-SecondValueSpecifies optional additional information about AliasProperty, ScriptProperty, or CodeProperty members. If used when adding an AliasProperty, this parameter must be a data type. A conversion 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 using the corresponding AliasProperty. For a CodeProperty, the value must be a reference to a method that implements a Set accessor. Use the GetMethod() method of a type reference to get a reference to a method. The method must take a single parameter that's a PSObject. The Get accessor is assigned using the Value parameter. For a ScriptProperty, the value must be a scriptblock that implements a Set accessor. The Get accessor is assigned using the Value parameter.
-TypeNameSpecifies 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 InputObject is a PSObject. This parameter was introduced in Windows PowerShell 3.0.
-ValueSpecifies the initial value of the added member. If you add an AliasProperty, CodeProperty, or ScriptProperty member, you can supply additional information using the SecondValue parameter. - For an AliasProperty, the value must be the name of the property being aliased. - For a CodeMethod, the value must be a reference to a method. Use the GetMethod() method of a type reference to get a reference to a method. - For a CodeProperty, the value must be a reference to a method that implements a Get accessor. Use the GetMethod() method of a type reference to get a reference to a method. reference. The method must take a single parameter that's a PSObject. The Set accessor is assigned using the SecondValue parameter. - For a ScriptMethod, the value must be a scriptblock. - For a ScriptProperty, the value must be a scriptblock that implements a Get accessor. The Set accessor is assigned using the SecondValue parameter.