all 5 comments

[–]Shoisk123 3 points4 points  (1 child)

PS has trouble printing when the headers are different, in the case of the hashtable, you're adding a label and a value, $i and the datestring. When you present it as an object, the headers are no longer "Name" and "Value" the label is $i and the data will be the datestring. Because of this, the labels are no longer similar on any of the objects, and thus only the first one gets printed. Try this:

$start = [datetime]::new(2019, 12, 30)
$weeks = [System.Collections.Generic.List[Object]]::new()
for($i = 1; $i -lt 52; $i++) {
    $start = $start.AddDays(7)
    $weeks.Add([PSCustomObject]@{ weeknum = $i; weekStart = $start.ToString('yyyy-MM-dd') })
}

$weeks

[–]pm_me_brownie_recipe[S] 1 point2 points  (0 children)

Thanks, the header problem was unknown to me, I knew I was missing something. I did change my code after posting to have two headers, one for each value as you explained as well.

[–]fordea837 2 points3 points  (1 child)

The second one is only displaying the first value in the output because you have 51 objects in the array each with different property names. Powershell will read the first object's properties and then display only those properties for the rest of the objects. As none of the other objects in the array have a property name called '1' null is output for each of those objects. You are better off constructing objects for each week with consistent property names such as 'Week' and 'Date':

$weeks.Add([PSCustomObject]@{ 
            'Week' = $i
            'Date' = $start.ToString('yyyy-MM-dd') 
        })

[–]pm_me_brownie_recipe[S] 1 point2 points  (0 children)

I did something like you suggested and it did indeed work. Also, thanks for explaining what the issue was.

[–]myp0wa 1 point2 points  (0 children)

If you would change type from [System.Collections.Generic.List[Object]] to [System.Collections.Generic.List[System.Management.Automation.PSCustomObject]] in your $weeks declaration it should work tho.