# Add-member to object in loop

**URL:** <https://forums.powershell.org/t/add-member-to-object-in-loop/15050>\
**Category:** PowerShell Help\
**Created:** [October 5, 2020, 10:37am UTC](https://forums.powershell.org/t/add-member-to-object-in-loop/15050 "2020-10-05T10:37:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasps](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@thomasps](https://forums.powershell.org/u/thomasps)\
**Post date:** [October 5, 2020, 10:37am UTC](https://forums.powershell.org/t/add-member-to-object-in-loop/15050/1 "2020-10-05T10:37:33Z")

</div>

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/2/28d73ee11888d15789442336a3fee7c19006ab19.png)

&nbsp;

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.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [October 5, 2020, 2:53pm UTC](https://forums.powershell.org/t/add-member-to-object-in-loop/15050/2 "2020-10-05T14:53:01Z")

</div>

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}}
```

---

<div class="post-metadata">

**Author:** ![thomasps](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@thomasps](https://forums.powershell.org/u/thomasps)\
**Post date:** [October 5, 2020, 9:15pm UTC](https://forums.powershell.org/t/add-member-to-object-in-loop/15050/3 "2020-10-05T21:15:07Z")

</div>

Thanks. Works perfect 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/add-member-to-object-in-loop/15050/4 "2024-05-16T20:29:34Z")

</div>


