Column of CSV, name vs constructed name

Hi, my problem is a bit strange and I don’t understand why.

Basically I have a CSV with every day of the year as column and some numeric values inside, I’d like to select specific values using a for loop.

In my array here I put only the days of January for semplicity.

$VenditeGrezze = "C:\Users\bm607134\OneDrive - Brescia Mobilità S.p.A\Desktop\bk tvm"
$FileAggregato = Get-ChildItem -Path $VenditeGrezze -Include "*Vendite*" -Recurse |
        Sort-Object LastWriteTime | 
        Select-Object -Last 1

$Aggregato = Import-Csv -Path $FileAggregato -Delimiter ';' -ErrorAction Stop

$Calendario2 = @(‘2026-01-01’,’2026-01-02’,’2026-01-03’,’2026-01-04’,’2026-01-05’,’2026-01-06’,’2026-01-07’,’2026-01-08’,’2026-01-09’,’2026-01-10’,’2026-01-11’,’2026-01-12’,’2026-01-13’,’2026-01-14’,’2026-01-15’,’2026-01-16’,’2026-01-17’,’2026-01-18’,’2026-01-19’,’2026-01-20’,’2026-01-21’,’2026-01-22’,’2026-01-23’,’2026-01-24’,’2026-01-25’,’2026-01-26’,’2026-01-27’,’2026-01-28’,’2026-01-29’,’2026-01-30’,’2026-01-31’)


$ArrayName2 = "'" + $Calendario2[2] + "'"
Write-Output $ArrayName2
Write-Output $Aggregato[2].$ArrayName2
Write-Output $Aggregato[2].'2026-01-03'

The import is successful, what I don’t understand is this:

If I write

Write-Output $Aggregato[2].'2026-01-03'

It works, while if I write

Write-Output $Aggregato[2].$ArrayName2

Which should be the same thing it doesn’t

If I print $ArrayName2 I see ‘2026-01-03’ like it is supposed to

Am I missing something?

Hmmm … I would actually expect this to work just fine …

$Aggregato[2].$Calendario2[2]

I tried doing this and it returns an error:

Cannot index into a null array.

Ok, now it works, I don’t understand what changed. I’m pretty sure before it wasn’t printing anything

$ArrayName2 = "'" + $Calendario2\[2\] + "'" results in a string value that includes the single quotes and has a length of 12 characters.
Your use of ‘2006-01-03’ (for example) results in a string value that does not include the single quotes and has a length of 10 characters.
The values in the $Calendario2 array (which, I assume, are the column header names in the CSV) don’t include the single quotes. The single quotes in your code that initializes $Calendario2 protect the string value of, say, “2026-01-03” from being interpreted as an expression that subtracts 1 from 2026 and 3 from the result of that subtraction.

1 Like