Powershell Class collection question

I have been working on a scripting project that involves creating custom powershell classes. I am currently trying to understand the following syntax

$Temp = [myclass[]]::new(1)

This syntax seems to create a generic List of some kind where I can specify the size of the List. Is there any way for me to modify the constructor for this? Ideally I would like to do something like the following:

[string[]]$ClassData = @('test1','test2','test3')
[myclass[]]$Temp = [myclass[]]::new($ClassData)

And this would call the constructor for myclass for each of the items in $ClassData. Unfortunately this is not the way that it works currently and I have been working around the situation by using the following:

[string[]]$ClassData = @('test1','test2','test3')
[myclass[]]$Temp = @($ClassData | [myclass[]]::new($_))

Any assistance or explanations as to how to make all this work would be greatly appriciated


What you are trying to achieve is to create multiple instances of a class. AFAIK, this cannot be achieved without looping.

A small correction in your code, you have to use foreach.

[myclass[]]$Temp = $ClassData | ForEach-Object -Process {[myclass[]]::new($_)}

@kvprasoon I have since found out that

[myclass[]]$Temp = $ClassData

will meet the simple example that I used and calls the class constructor with the corresponding data from the $ClassData array. So powershell basically handles the foreach loop automatically using that syntax. I am still experimenting with calling more complex constructors

Are you sure on [myclass]$Temp = $ClassData ? can you post the class definition.

Here is an example to illustrate my point

class MyClass {
    [string]$Name = ''
        $this.Name = $Data

$Data = @('This','Is','A','Test')
[MyClass[]]$Temp = $Data

$Temp[0] | GM

$Temp  | FT

This will yield the following results

TypeName: MyClass

Name        MemberType Definition                    
----        ---------- ----------                    
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()             
GetType     Method     type GetType()                
ToString    Method     string ToString()             
Name        Property   string Name {get;set;}