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
PropertyorMethod. - -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.
Related Articles
Core Object Manipulation
- PowerShell Select-Object - Select and transform properties
- PowerShell ForEach-Object - Process objects in pipeline
- PowerShell Where-Object - Filter objects
- PowerShell Variables - Store custom objects
Data Structures
- PowerShell Arrays - Work with object arrays
- PowerShell Hashtables - Create custom objects
- PowerShell Strings - String manipulation in objects
Control Flow & Logic
- PowerShell If-Else Statement - Conditional object handling
- PowerShell Switch Statement - Switch logic in objects
- PowerShell Try-Catch - Error handling with objects
Function Integration
- PowerShell Functions - Use Add-Member in functions
- PowerShell Output to File - Output custom objects
Data Export & Conversion
- PowerShell Export CSV - Export custom objects to CSV
- PowerShell Import CSV - Create objects from CSV
File Operations
- PowerShell Get File Properties - Add properties to file objects
- PowerShell List Files - Extend file object properties
System Operations
- PowerShell Get-Process - Add properties to process objects
- PowerShell Get-Service - Extend service objects
Active Directory
- PowerShell Active Directory Guide - Add properties to AD objects
- DSACLS Permission Management - Work with AD object properties
Performance & Output
- PowerShell Format Table - Display custom properties
- PowerShell Output Table - Format custom objects
- PowerShell Measure-Object - Aggregate custom objects
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell with object handling
- Complete PowerShell Tutorial - Comprehensive course
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.