pipe multiple foreach resutls into a two-column array

piping a list of AD servers into a foreach, , and looking on each server for a Symantec program folder with a certain name. I want to output a list of $server.name and $programfolder.name. this is easy enough.

foreach ($server in $servers) {
$folder = “\$($server.name)\c$\Programdata\symantec\symantec endpoint protection”
$programfolder = get-childitem $folder | ? {$.name -like “12*” -or $.name -like “11*”}
$server.name + " " + $programfolder.name

but instead of the rudimentary implied ‘write host’, how do instead feed the list into an two-column array, with $server.name in one column and $programfolder.name in the other column? I’ve gotten as far as $array = @(), but even in my basic testing without the rest of my script, I can’t get it to work. the following

$array = @()
$array +=@(‘test1’,‘test2’)

creates a one-column array with values of test1 and test2, which makes sense. how do I get an array with two columns?

Im new to Powershell this is how I do it.

$array = @()
$array += New-object  PSObject -Property([ordered]@{Item1Name = $item1; Item2Name= $item2})

if you put it inside of your loop it will keep adding new items. then to access an item you can $array.Item1Name[0] or loop though them by doing a foreach(item in $array){
Do somthing with each one


There are a few different ways to do it. Here’s one. This one requires PowerShell 3.0 or higher.

$array = @()
foreach ($server in $servers) {
$folder = "\\$($server.name)\c$\Programdata\symantec\symantec endpoint protection"
$programfolder = get-childitem $folder | ? {$_.name -like "12*" -or $_.name -like "11*"}
$array += [pscustomobject]@{ Server = $server.name; Folder = $programfolder.name }

(By the way, it’s an implied Write-Output, not an implied Write-Host. Which is good. Don’t use Write-Host.)

Mark’s got the right approach. PowerShell works best with objects, so producing an object that has properties is the best approach. That’ll make the output usable by the rest of the shell for formatting, exporting, manipulation, or whatever.

thanks everybody. my specific end goal was to produce a list ($array | out-gridview) of servers with the size of their Symantec folders (we had a couple of instances where Symantec was not cleaning up its old definitions.). and while I was at it, why not throw in the version of Symantec (the folder name) to make sure it was up to date. here is what I ended up using:

foreach ($server in $servers) {
    $folder = "\\$($server.name)\c$\Programdata\symantec\symantec endpoint protection"
    $programfolder = get-childitem $folder |? {$_.name -like "12*" -or $_.name -like "11*"}
    $bytes = (get-childitem $folder -Recurse -ea silentlycontinue| measure length -sum)
     $size = [math]::Round(($bytes.sum/1mb),2)
        $row = new-object psobject
        $row | add-member -type NoteProperty -name 'Server' -Value $server.Name
        $row | add-member -Type NoteProperty -name 'Version' -Value $programfolder.Name
        $row | add-member -Type NoteProperty -name 'Size' -Value $size
                $array += $row
$array | Out-GridView

i’m not sure where I found the ‘noteproperty’ thing, since i don’t see it mentioned it on this thread… but it’s working for what I want. is there any disadvantage to using ‘noteproperty’?

John, what you are doing there is a perfectly valid way of doing it. The different ways that are shown in this thread are from different releases of PowerShell. Check out this link to see how it has changed over time.


You are doing it the PowerShell 1.0 way, Mark the PowerShell 2.0 way, and Tim the PowerShell 3.0 way.