Unable to display multiple PSCustomObjects in an Array list

I have 3 folders in the same folder, with files in it; only the 3 sub-folders have anything in them.

What I'm trying to accomplish is:

  1. Query the root folder to get the sub-folders within it (this allows it to be more dynamic for me in case I want to add more).
  2. Query each folder individually, assigning all output files (with the current loop iteration number concatenated to the name) into a PSCustomObject.
  3. Then, append each newly created PSCustomObject to my $myArray variable - the name property for each object is based on the current folder name.
  4. Finally, display the arraylist of PSCustomObject's in Table format.

With the expected result of:

subfolder1        subfolder2    subfolder3      
----------        ----------    -----------      
1: 1              4: 2          6: 3             
2: New folder     5: New folder 7: New folder    
3: New folder (2)               8: New folder (2)

What I currently get is just the first object in my array:

subfolder1       
----------       
1: 1             
2: New folder    
3: New folder (2)

...but, I can use the comma as a unary operator and it will show the other objects in the list. Same goes for tapping into the arrays intrinsic members of .psobject and it will show the objects there as well.

Enough back story, here's my code:

Get-ChildItem -Path "C:\Users\Abraham\Desktop\test" -Directory | 
    ForEach-Object -Begin {
        $myArray = [System.Collections.ArrayList]::new()
        $i = 0
    } -Process {
        foreach ($folder in $input)
        {
            $name = $folder.BaseName
            $myArray.Add(
                $(foreach ($file in (Get-ChildItem -Path $folder.FullName))
                {
                    $i++
                    [PSCustomObject]@{
                        $name = "{0}: {1}" -f $i,$file.Name
                    }
                }) 
            ) | Out-Null
        }
    } -End { 
        $myArray
    }

Question: Any ideas on how to get the appended PSCustomObject's be displayed in table format?


What's odd is that I can use Select-Object -First 1/2/3, and will be able to access each object. Can also do: $myArray.foreach{$_ | ft *} but, it flattens it so I can no longer display the combined results as a table; any other method wasn't working for me such as piping to Write-Output.


Solution 1:

Basically, what your code is doing, is storing the contents of each folder in an Object[] where $myArray[0] are all the contents of subFolder1, $myArray[1] are all the contents of subFolder2, and so on. What you're missing is the part where you merge or join each element of each array into one PSCustomObject, like below:

[pscustomobject]@{
    subfolder1 = '2: New folder'
    subfolder2 = '5: New folder'
    subfolder3 = '7: New folder'
}

Here is my solution to that problem:

using namespace System.Collections.Generic
using namespace System.Collections

(Get-ChildItem -Path "C:\Users\Abraham\Desktop\test" -Directory).foreach({
    begin {
        $myArray = [Dictionary[string, ArrayList]]::new()
        $i = 0
    }
    process {
        foreach ($file in Get-ChildItem -Path $_.FullName)
        {
            $i++
            $file = "{0}: {1}" -f $i,$file.Name
            if($thisKey = $myArray[$_.BaseName])
            {
                $null = $thisKey.Add($file)
                continue
            }
            $null = $myArray.Add($_.BaseName, @($file))
        }
    }
    end {
        $max = $myArray.Keys.ForEach({ $myArray[$_].Count }) | Measure-Object -Maximum
        for($i = 0; $i -lt $max.Maximum; $i++)
        {
            $out = [ordered]@{}
            foreach($key in $myArray.Keys)
            {
                $out[$key] = $myArray[$key][$i]
            }
            [pscustomobject]$out
        }
    }
})