Search code examples
arrayspowershell

Powershell: How to add new values to an array


I have an Netapp .csv containing volume names, size, etc. and I'm importing this into an Powershell array $volumes.

I'm filtering the stuff I need:

$volumes | select Name,"Storage VM",Capacity

However, the "Capacity" is in Byte and I'd like to display in GB. I know how to convert, but I don't know how to add the converted item into the existing array:

foreach ($vol in $volumes) { $volsizeGB = $vol.Capacity/1GB }

How do I add this "$volsizeGB" to the $volumes?

Thanks


Solution

  • If you want to update the property value of each Capacity property:

    foreach ($vol in $volumes) { $vol.Capacity = $vol.Capacity / 1GB }
    

    If you want to add a new property with the formatted capacity it's better to use Select-Object with a calculated property:

    $volumes | Select-Object @(
        'Name'
        'Storage VM'
        'Capacity'
        @{ N='FormattedCapacity'; E={ $_.Capacity / 1GB }})