Add-member to object in loop

Hi,

Im new to working with objects so please bear with me. I have created a powershell script that calls an API to get some data.
The data is in a System array with Cusom objects and I need to add new member with uniqe values for each object.
I have only managed to add the same member value to each object. Can someone please help me with a solution for this and maybe have a link to good reading about objects like this.

My system array / objects:
Data

 

Code:

$response | ForEach-Object {

$capacity = ($_.capacitybytes) / 1GB
$free = ($_.freespacebytes) / 1GB
$used = ($capacity - $free)


$response |  Add-Member -NotePropertyName UsedSpaceGB -NotePropertyValue $used -Force

}


$response

Add-member is adding the same value to all objects. How can I add-member or noteproperty on each object with the correct value?
Later on I also want to update other members with new values.

thanks, and sorry for bad explanation. I have not very much experience as you might see.

You are adding it to the collection $response instead of the current item $_

$response | ForEach-Object {

$capacity = ($_.capacitybytes) / 1GB
$free = ($_.freespacebytes) / 1GB
$used = ($capacity - $free)


$_ |  Add-Member -NotePropertyName UsedSpaceGB -NotePropertyValue $used -Force

}

$response

You could also create a new custom object with the additional calculated property instead of a foreach loop

$response | Select-Object *,@{n='UsedSpaceGB';e={$_.capacitybytes - $_.freespacebytes / 1GB}}

Thanks. Works perfect :slight_smile: