Powershell add-member. Add a member that’s an ArrayList?

Question:

The Powershell “add-member” command is very useful. I use it to add properties to custom objects. Sometimes I set a member as an array to hold multiple objects. Is it possible to add an ArrayList as a member on a custom object?

Imagine a list of articles has properties “index”, “title”, and “keywords.” In Powershell, you could put this code in a loop:

You’d end up with an array, $articles, of Article objects, each with members index, title, and keywords. Furthermore, the keywords member is an array that can have multiple entries:

This meets most of my needs, but I just don’t like dealing with arrays. ArrayLists are just easier to work with, if only because

is so much more intuitive than

Is there a way with Add-Member to add an ArrayList as a member? Or am I stuck with arrays? If Powershell doesn’t support thi snatively, could I pop in some C# code to make a new class with an ArrayList as a member?

Answer:


To add a blank ArrayList to your custom object just use

Source:

Powershell add-member. Add a member that’s an ArrayList? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply