Home » Add-Member – PowerShell Cmdlet

Add-Member – PowerShell Cmdlet

PowerShell’s Add-Member cmdlet is a powerful tool that enables you to add custom members such as properties and methods to existing object. This allows you to extend the functionality or properties of objects dynamically.

In this article, we will discuss about Add-Member cmdlet, its syntax, and practical examples.

Add-Member Cmdlet Syntax

The basic syntax for the Add-Member cmdlet is as follows:

Add-Member [-InputObject] <PSObject> [-MemberType] <string> [-Name] <string> [-Value] <Object> [-PassThru] [-Force] [-TypeName <string>] [-SecondValue <Object>] [<CommonParameters>]
  • -InputObject: Specifies the object to which the new member is added.
  • -MemberType: Specifies the type of the member being added, such as Property or Method.
  • -Name: Specifies the name of the new member.
  • -Value: Specifies the value of the new member.
  • -PassThru: Returns the object with the new member added.
  • -Force: Allows overwriting existing members with the same name.
  • -TypeName: Specifies the type name of the new member. This is optional and typically used for properties.
  • -SecondValue: Specifies the second value of the new member. This is used when adding a property with multiple values.

Let’s understand the PowerShell Add-Member cmdlet with practical examples.

How to Add a Property to an Object

The following example adds a City note property with a value of “New York” to the $object represents a custom object with predefined properties and their values.

The first command creates the custom object with Name and Age properties and its value and assign it to the $object variable.

The second command adds the note property “City” to the object in $object.

The third command uses dot notation to get the value of the City property of the object in $object.

$object = [PSCustomObject]@{ Name = "Gary"; Age = 30 }

$object | Add-Member -MemberType NoteProperty -Name "City" -Value "New York"

$object.City

Result:

New York

How to Add a Script Method to an Object

To add a script method to an object, first creates an object with predefined properties and values and assign it to $object variable.

The Add-Member cmdlet uses the -MemberType parameter to specify ScriptMethod to specify the Name as Greet and Value to it.

$object = [PSCustomObject]@{ Name = "Gary"; Age = 30 }
$object | Add-Member -MemberType ScriptMethod -Name "Greet" -Value { "Hello, $($this.Name)!" }

$object.Greet()

Result:

Hello, Gary!

How to Add an Alias Property to a PSObject

The following example adds a Size alias property to the object that represents the log.txt file. The new property Size is an alias for the Length property.

In the below script, it uses -MemberType parameter to specify AliasProperty to add Size property with value Length to the $file object.

$file = Get-ChildItem C:\temp\1.txt

$file | Add-Member -MemberType AliasProperty -Name Size -Value Length

$file.Size

Result:

1854

Conclusion

I hope the above article on how to use Add-Member cmdlet in PowerShell is helpful to you. This command enables you to extend the capabilities of PowerShell objects by adding custom properties and methods.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.