Search code examples
powershellpowershell-2.0powershell-3.0powershell-remoting

how to fetch value at particular location and also to set value in a LIst of integer in powershell?


code i m trying is :-

$sumList1= New-Object System.Collections.Generic.List[int]
$sumList1.Add(1)
$sumList1.Add(2)
$sumList1.Add(3)
$sumList1.Add(4)

$i = $sumList1.Items[1];
write-host $i

please help me on this.


Solution

  • You made a typo, it is Item(), not Items[]: $sumList1.Item(1)

    Or use normal index access: $i = $sumList1[1]

    To change the value of an existing item use $sumList1[1] = 5, if you want to add an element use the Add method as in your sample code.