# create a hashtable with foreach

**URL:** <https://forums.powershell.org/t/create-a-hashtable-with-foreach/3297>\
**Category:** PowerShell Help\
**Created:** [October 21, 2014, 8:35pm UTC](https://forums.powershell.org/t/create-a-hashtable-with-foreach/3297 "2014-10-21T20:35:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![democracy77](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@democracy77](https://forums.powershell.org/u/democracy77)\
**Post date:** [October 21, 2014, 8:35pm UTC](https://forums.powershell.org/t/create-a-hashtable-with-foreach/3297/1 "2014-10-21T20:35:34Z")

</div>

Hi NG,

I’m stuck currently with this simple task but I don’t understand what all the articles want to explain me.

I’m collecting a bunch of XenServer pool master IP addresses from XenDesktop. Then I want to loop through each of the IP addresses to get the corresponding hypervisorconnectionname. Sometime there can be more than 1 hypervisorconnectionnames for an IP address.

My issue is that the hash table only contains the last hypervisorname instead of all. What am I doing wrong here?

# Get all Poolmaster IPs from XenDesktop broker

$poolmasterips = get-ChildItem xdhyp:\connections -AdminAddress $stradminaddress | select -ExpandProperty HypervisorAddress -Unique

foreach ($poolmasterip in $poolmasterips)  
{

# Get hypervisorconnection names from pool master IP

$hypervisorname = get-childitem xdhyp:\connections -AdminAddress $stradminaddress | where {$\_.HypervisorAddress -eq $poolmasterip} | select -ExpandProperty HypervisorConnectionName

$hypervisornamelist = @{

name = $hypervisorname  
}  
}

Regards

Christian

---

<div class="post-metadata">

**Author:** ![simon-wahlin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/simon-wahlin/32/407_2.png) [@simon-wahlin](https://forums.powershell.org/u/simon-wahlin)\
**Post date:** [October 21, 2014, 9:26pm UTC](https://forums.powershell.org/t/create-a-hashtable-with-foreach/3297/2 "2014-10-21T21:26:47Z")

</div>

Hi Christian,

You are overwriting the variable $hypervisorname in each iteration of your loop.

There are a few ways to solve this, the easiest in my opinion is to do this:

```
$Hypervisorlist = foreach ($poolmasterip in $poolmasterips)
{
   $hypervisorname = get-childitem xdhyp:\connections -AdminAddress $stradminaddress | where {$_.HypervisorAddress -eq $poolmasterip} | select -ExpandProperty HypervisorConnectionName
   @{
      name = $hypervisorname
   }
}
```

This will write the hash-table to the output stream, which will end up in the collection $Hypervisorlist

You might want to consider using PSCustomObjects instead of HashTables.

---

<div class="post-metadata">

**Author:** ![democracy77](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@democracy77](https://forums.powershell.org/u/democracy77)\
**Post date:** [October 21, 2014, 9:46pm UTC](https://forums.powershell.org/t/create-a-hashtable-with-foreach/3297/3 "2014-10-21T21:46:01Z")

</div>

Thanks Simon! That worked.

I will checkout the PSCustomObjects subject now.

Thanks again.

Regards

Christian

---

<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:46pm UTC](https://forums.powershell.org/t/create-a-hashtable-with-foreach/3297/4 "2024-05-16T20:46:50Z")

</div>

Closed on May 16, 2024, 8:46pm UTC


