Powershell

How to Use Add-Member (Microsoft.PowerShell.Utility) Cmdlet in PowerShell?

In PowerShell, the “Add-Member” cmdlet is used to add custom methods and properties to an instance of an object. It is very useful when adding data in a particular manner that is not supported. For instance, if a user wants to add a custom field of data that was missed at the initial stage. Then, the specified cmdlet can be used.

In this post, the “Add-Member” cmdlet in PowerShell will be illustrated.

How to Use/Utilize the PowerShell’s Add-Member (Microsoft.PowerShell.Utility) Cmdlet?

To add the member to an instance of the object, pipe it to the “Add-Member” cmdlet. Moreover, the properties can be used to add properties to an object. Let’s have a look at the provided examples to understand more about the stated cmdlet.

Example 1: Use the “Add-Member” Cmdlet to Add a Note to the Property

Execute the below-given cmdlet to add a note to the property in PowerShell:

$person = [PSCustomObject] @{
    Name = "John Patrick"
    Age = 30
}
$person | Add-Member -NotePropertyName "Profession" -NotePropertyValue "Doctor" -PassThru

 
According to the above-given code:

    • Create a variable and assign it to the hash table having the stated values.
    • Next, place the hash table assigned variable and pipe it to the cmdlet “Add-Member”.
    • Then, specify the “-NotePropertyName” and “-NotePropertyValue” having the stated values assigned to them.
    • Lastly, mention the “-PassThru” parameter:

 

Example 2: Use the “Add-Member” Cmdlet to Add a String Note Property to a String

Run this command to add a note property to a string:

$str = "Its a string."
$str = $str | Add-Member -NotePropertyMembers @{String = "Display text"} -PassThru
$str.String

 
In the above-stated code:

    • First, initialize a variable and provide it a string value.
    • Then, pipe the string and assign a variable to the “Add-Member” cmdlet.
    • Next, specify the “-NotePropertyMembers” parameter and assign it the hash table value alongside the “-PassThru” parameter.
    • Lastly, concatenate the string-assigned variable with the hash table value:

 

Example 3: Use the “Add-Member” Cmdlet to Add an Alias Property to a PSObject

To add a property to a PSObject, execute the following command in PowerShell:

$str1 = Get-ChildItem C:\Docs\New_File.txt
$str1 | Add-Member -MemberType AliasProperty -Name Size -Value Length
$str1.Size

 

That’s all! You have successfully learned about the “Add-Member” cmdlet in PowerShell.

Conclusion

PowerShell’s cmdlet “Add-Member” is used to add a member to an object’s instance, such as methods and properties. For example, users can use the “-NoteProperty” parameter to add details about a specific cmdlet. This blog has provided details about the “Add-Member” cmdlet in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.