You basically want a hash table with values that are arrays. You don't have to use $hashtable.get_item or .add

$myHashTable = @{} # creates hash table
$myHashTable.Entry1 = @() #adds an array
$myHashTable.Entry1 += "element1"
$myHashTable.Entry1 += "element2"

This results in the following output:

$myHashTable

Name                           Value                                                                                                                                                                                           
----                           -----                                                                                                                                                                                           
Entry1                         {element1, element2}

$myHashTable.Entry1
element1
element2
Answer from Jason Snell on Stack Overflow
🌐
PowerShell Forums
forums.powershell.org › powershell help
Create HashTable With Multiple Values - PowerShell Help - PowerShell Forums
April 24, 2020 - Hi, I think i need a hash table for this? I would like a script to loop through a hash table and for each line, pass the values to my function. I have a working solution for 2 columns or a key and a value i believe?: $accounts = @ item1 = 'string_value1' item2 = 'string_value2' item3 = 'string_value3' } foreach ($line in $accounts.GetEnumerator()){ Get-MyBalanceFunction -account $($line.Name) -string $($line.Value) } My issue is trying to add a second value, I would like the list to have 3...
Discussions

Need assistance with a hashtable that returns multiple values for a key?
The value of the 'Groups' property appears to be an array, whose values could be joined for display purposes with a newline character as separator, for example: $userObj.Groups -join "`n" More on reddit.com
🌐 r/PowerShell
1
1
September 14, 2022
How to have multiple values for one key in hashtable from csv
I’m looking for a data structure that will allow me to add multiple values to a single key in PowerShell that reads from a csv. I am currently using a hashtable and I have it almost figured out but am stuck on how to make the key with the array values. My CSV looks like: ID Name 1234 John ... More on community.spiceworks.com
🌐 community.spiceworks.com
11
5
January 21, 2020
Create HashTable With Multiple Values
Hi, I think i need a hash table for this? I would like a script to loop through a hash table and for each line, pass the values to my function. I have a working solution for 2 columns or a key and a value i believe?: $accounts = @ item1 = 'string_value1' item2 = 'string_value2' item3 = ... More on forums.powershell.org
🌐 forums.powershell.org
0
0
April 24, 2020
Hashtables and Key-Pair, trying to have additional values per key.
If you want to do something different than a hash of hashes, this may be a good chance to play with classes and objects. You can define an object as you need, and put it into a collection: class SiteResult { [string]$site [datetime]$date [double]$ver } $results = @{} for ($i=1; $i -le 10; $i++){ $t_result = New-Object SiteResult $t_result.site = "TestSite" $t_result.date = get-date $t_result.ver = $i $results.add("TestSite$i", $t_result) } Name Value ---- ----- TestSite8 SiteResult TestSite5 SiteResult TestSite7 SiteResult TestSite4 SiteResult TestSite1 SiteResult TestSite3 SiteResult TestSite9 SiteResult TestSite2 SiteResult TestSite10 SiteResult TestSite6 SiteResult In the end I would not say it's more complex than a hash of hashes or array, just different. More on reddit.com
🌐 r/PowerShell
17
9
April 12, 2016
🌐
Reddit
reddit.com › r/powershell › hashtable with multiple values per key - how to get proper values count?
r/PowerShell on Reddit: Hashtable with Multiple Values per Key - How to get proper Values count?
October 25, 2020 -

Learning PS so please bear with me.

Assume I have a hash-table which has multiple values assigned per key, how do I get an accurate count of the number of values in total in the table?

When trying to count below I'm expecting to get an answer of 4, not 3.

PS C:\WINDOWS\system32> $TestHT = [ordered]@{1=1;2=2;3=3,4}

PS C:\WINDOWS\system32> $TestHT

Name                           Value                                           
----                           -----                                           
1                              1                                               
2                              2                                               
3                              {3, 4}

PS C:\WINDOWS\system32> $TestHT.Values.Count
3

PS C:\WINDOWS\system32> $TestHT.Values.GetEnumerator()
1
2
3
4

PS C:\WINDOWS\system32> $TestHT.Values.GetEnumerator().Count
1
1
2

Do I really have to do something like

PS C:\WINDOWS\system32> $TestHT.Values.GetEnumerator().Count | Measure-Object -Sum | Select-Object Sum

Sum
---
  4

There must be a cleaner, better way? Also how do I get just the sum as an integer if that's how I must do it?

🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › learn › deep-dives › everything-about-hashtable
Everything you wanted to know about hashtables - PowerShell | Microsoft Learn
Generally, you think of a hashtable as a key/value pair, where you provide one key and get one value. PowerShell allows you to provide an array of keys to get multiple values.
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-multiple-values-in-the-hashtable-using-powershell
How to add multiple values in the Hashtable using PowerShell?
March 30, 2021 - For example, we have a below-created hashtable. PS C:\> $servicehash = @{Name='Spooler';State='Stopped';StartType='Automatic'} PS C:\> $servicehash · Name Value ---- ----- Name Spooler StartType Automatic State Stopped · We need to add multiple values to the Name Key.
🌐
Itechguides
itechguides.com › home › technology explained › windows powershell explained › powershell hashtable ultimate guide with examples
PowerShell Hashtable Ultimate Guide with Examples - Itechguides
April 18, 2024 - In this instance, you effectively want to create a hashtable with arrays as the values. To begin, I think it is better to show you the codes I used to differentiate a hashtable from an array. $hashtable = @{ Name = "Document" PSProvider = "FileSystem" Root = "C:\Users\victo\Documents" } $array = @("Document", "FileSystem", "C:\Users\victo\Documents") So, to create a PowerShell hashtable with single keys and multiple values, run a script similar to this:
🌐
LazyAdmin
lazyadmin.nl › home › powershell hashtable – everything you need to know
PowerShell HashTable - Everything you need to know — LazyAdmin
October 4, 2022 - So a better way to structure your data when you have multiple entries is to use objects. The easiest way to create objects in PowerShell is to convert a hashtable to a custom PowerShell object.
Find elsewhere
🌐
Reddit
reddit.com › r/powershell › need assistance with a hashtable that returns multiple values for a key?
r/PowerShell on Reddit: Need assistance with a hashtable that returns multiple values for a key?
September 14, 2022 -

Hi everyone,

My team and I are using PS Universal and absolutely loving it! At the moment I have a couple of Pages created which make use of APIs to gather some AD user info and display it. Works great except I'm getting hung up on one part. Within a hashtable I have the following:

param(
    [Parameter(Mandatory)]
    $Sam
)

$User = Get-ADUser -Identity $Sam -Properties *

$userObj = @{ }
$userObj.Name = ""
$userObj.Email = ""
$userObj.firstName = ""
$userObj.lastName = ""
$userObj.Id = ""
$userObj.Groups = @()

$User | % {
        $userObj.Name = $User.Name
        $userObj.Email = $User.UserPrincipalName
        $userObj.firstName = $User.GivenName
        $userObj.lastName = $User.Surname
        $userObj.Id = $User.SID.value
        $userObj.Groups = $User.MemberOf | % {
            @{
                GroupName = $_
            }
        }

    $importDetails = New-Object -TypeName PSObject -Property $userObj
}
$userObj

Now, the issue I am running into is on the PS U page I have a table that I created and am calling one column specifically from the generated hashtable, Groups. When I do this, the data is certainly there, but it looks horrific in the table on the PS U web page! Its basically all of the AD group membership CNs for the user, but in one LONG string. I would like to list each group on a separate line in the table. Is that possible? Thanks!

🌐
Spiceworks
community.spiceworks.com › programming & development
How to have multiple values for one key in hashtable from csv - Programming & Development - Spiceworks Community
January 21, 2020 - I’m looking for a data structure that will allow me to add multiple values to a single key in PowerShell that reads from a csv. I am currently using a hashtable and I have it almost figured out but am stuck on how to make the key with the array values. My CSV looks like: ID Name 1234 John 1234 John 1235 Jane 1236 Bob I have tried the following: $myCSV = Import-Csv “X:\Path\To\My\CSV\File.csv” $myCSV | select ID, Name -unique However, let’s say you add another ID 1234 for Name Jeff .
🌐
PowerShell Forums
forums.powershell.org › powershell help
Create HashTable With Multiple Values - #1000 - PowerShell Help - PowerShell Forums
April 24, 2020 - Hi, I think i need a hash table for this? I would like a script to loop through a hash table and for each line, pass the values to my function. I have a working solution for 2 columns or a key and a value i believe?: $accounts = @ item1 = 'string_value1' item2 = 'string_value2' item3 = 'string_value3' } foreach ($line in $accounts.GetEnumerator()){ Get-MyBalanceFunction -account $($line.Name) -string $($line.Value) } My issue is trying to add a second value, I would like the list to have 3...
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_hash_tables
about_Hash_Tables - PowerShell | Microsoft Learn
January 19, 2025 - The keys and values in a hashtable can have any .NET object type, and a single hashtable can have keys and values of multiple types. The following statement creates a hashtable of process name strings and process object values and saves it in ...
🌐
Reddit
reddit.com › r/powershell › hashtables and key-pair, trying to have additional values per key.
r/PowerShell on Reddit: Hashtables and Key-Pair, trying to have additional values per key.
April 12, 2016 -

I find it always helps to understand the end goal of what I'm attempting to do, so here is a summary of my task and how I'm currently attempting to accomplish it. My understanding is that hashtables will not really accomplish what I want as they are key-pair, but hopefully someone knows how to get this to work the way I want:

  1. Problem: We have biweekly software deployments that require manual checking to confirm that build versions have been approved for production. We use an application called cruisecontrol to track our builds, and as part of our process we go through each individual build's notes and search for ticket numbers. On the tickets themselves, they have a check box that confirm if they are approved or not. As this is a manual process right now, and we push anywhere from 5-10 applications at a time, with as many as 20 builds on each, this is a pretty long process that can easily eat up an hour of two for one employee, if not more.

  2. Goal: I am approaching this problem in segments, my first checkpoint will be getting to the point of pulling out the ticket numbers from each build and displaying it in a list with two pieces of data linked to it. I want to report the URL that I pulled the ticket number from, the name of the link within cruisecontrol I would have to click on to check manually, and any ticket numbers within the build. After this is complete, I will be making a separate function that can read this output and check the ticketing system for approvals. I create everything as a function these days so that it's reusable as possible and I don't have to hardcode many variables, if any at all.

  3. Current Approach / Progress: My function is using invoke-webrequest on the builds dashboard. When I request data, I provide the application name, the build number being deployed, and the previously deployed version. On this page we have links to every build, in the following format '2016-01-01 00:00:00 (0000)' - the last (0000) being the build number. The links to builds are in descending order, with the latest build being on the top. With my webrequest I pull in all of the links and am able to narrow them down by class to get only the links I'm interested in. I place them in an array, and then use IndexOf to count through the array looking for my build numbers. After getting the positions I create 2 additional arrays with the href and outertext (contains link name) from my array of link information. I create a hash table linking the two arrays I've created, the href array being the key, and the outertext being a value. With this complete, I cycle through my hash table, going to each link, doing an additional webrequest and using select-string to search the rawcontent for a regex that pulls in the ticket numbers. Occasionally if the build ticket number is formatted differently, the regex search will not pull in the ticket number correctly. When this happens, I have it storing the value False where the ticonditioanl would be, and an additional conditional statement triggers a check that confirms if there is actually a ticket within the build. If this returns true, it will create a flag for that specific build indicating it requires a manual check. I intend to either adjust my regex to get everything in one pass as possible, but that is more of a phase two problem to work on in this segment.

  4. Problem: What I want to do is have an additional value attached to each key, so that I have everything in one place. On builds that the regex fails, I want to be able to push out the values of the link name and that a manual check is required. On the builds that are successful, I want to push out the link name and the ticket numbers.

  5. Solution: I'm aware that I could make the value an array, but I would prefer not to approach the problem this way. I know it's a completely viable solution, and I might end up doing that, but I'm already familiar with that concept and I would like to learn how to do this differently. I've considered making two hash tables, both of which use the href data as the key, and then holding the different values. If possible I'd like to avoid this solution as I feel it's a neater and less prone to errors if the data is all in one place. I know I could also setup an actual database to do this as well, but that seems like an extreme for something like this (I wont be doing that, I'll use an array first). If anyone has some ideas on how to approach this, or how to get hashtables (or the method I'm really looking for, as my understand hashtables wont do this) I'd really appreciate it!

Thanks for taking the time to read!

🌐
IDERA
community.idera.com › home › blogs
Blogs | IDERA
October 8, 2025 - Dive into cutting-edge articles, expert tips, and industry trends on Idera Software's blog. Elevate your database management journey today.
Top answer
1 of 4
2

If you want to add multiple values to the same key, you need to test for whether the key is already present and act accordingly:

$keys = 1,2,3,1
$value = 'a value'
$hashtable = @{}

foreach($key in $keys){
  if(-not $hashtable.ContainsKey($key)){
    # First time we see this key, let's make sure the value is a resizable array
    $hashtable[$key] = @()
  }

  # Now we can safely add values regardless of whether the key already exists
  $hashtable[$key] += $value
}
2 of 4
1

To complement the answer from @Matthias:
You can't have duplicate keys in a hash table. This is by design. Hash tables contain a list of unique keys based on a binary search algorithm where each key is linked to a specific object (value).

There are two workarounds:

1. add an array of values to a specific hash table key:

$Keys = 1,2,2,3
$Values = 'a'..'d'
$HashTable = @{}
$i = 0
foreach($key in $Keys){ [array]$HashTable[$Key] += $Values[$i++] }

Iterating
The disadvantage is that you lose the order of the items (regardless if you use an ordered dictionary simply because some values share a single key).

Foreach ($Key in $Hashtable.Keys) {
    Foreach ($Value in @($Hashtable[$Key])) {
        Write-Host $Key '=' $Value
    }
}

3 = d
2 = b
2 = c
1 = a

Searching
The advantage is that the items are binary indexed and therefor quickly found.

$SearchKey = 2
Foreach ($Value in @($Hashtable[$SearchKey])) {
    Write-Host $SearchKey '=' $Value
}

2 = b
2 = c

2. or you might create a list (array) of hash tables (Key Value Pairs)

$Keys = 1,2,2,3
$Values = 'a'..'d'
$i = 0
$KeyValuePairs = foreach($key in $keys){ @{ $Key = $Values[$i++] } }

Iterating
The advantage is that the items will stay in order.
(Note that you have to invoke the GetEnumerator() method twice)

$KeyValuePairs.GetEnumerator().GetEnumerator() | Foreach-Object {
    Write-Host $_.Key '=' $_.Value
}

1 = a
2 = b
2 = c
3 = d

Searching
The disadvantage is that searching for the items is slower and little more comprehensive.

$SearchKey = 2
$KeyValuePairs.GetEnumerator().GetEnumerator() | 
    Where-Object Key -eq $SearchKey | Foreach-Object {
    Write-Host $_.Key '=' $_.Value
}

2 = b
2 = c
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › 6a766bbb-6177-4dc7-bb3b-cd9f0a566ca5
PowerShell 2.0 hashtable storing multiple values for each key | Microsoft Learn
powershell does support hashtables here is a nice read : http://technet.microsoft.com/en-us/library/ee692803.aspx ... Grant Ward, a.k.a. Bigteddy ... Sri, you were very close. Just change this line: ... This way you end up with a hash table where each value is an array.
🌐
Stack Overflow
stackoverflow.com › questions › 66929336 › creating-multiple-values-hashtable-inside-powershell
arrays - Creating Multiple values hashtable inside powershell - Stack Overflow
Name : {18181a46-0d4e-45cd-891e-60aabd171b4e, 0c266dff-15dd-4b49-8397-2bb16070ed52} Value : O365_E1_Users Name : {6fd2c87f-b296-42f0-b197-1e91e994b900, 0c266dff-15dd-4b49-8397-2bb16070ed52} Value : O365_E3_Users
🌐
University of Texas at Austin
sites.utexas.edu › glenmark › 2022 › 05 › 24 › powershell-hash-tables
PowerShell Hash Tables | The AEMS Flogger
May 24, 2022 - A hash table is an array of key/value pairs, along with accompanying functions for readily accessing or modifying values by way of their corresponding keys. Here is the PowerShell syntax for initializing an empty hash table object:
🌐
Reddit
reddit.com › r/powershell › how to validate multiple levels of hashtable keys match a given hash?
r/PowerShell on Reddit: How to validate multiple levels of hashtable keys match a given hash?
July 22, 2022 -

I have a function that takes a hashtable as input with a specific structure. For example:

$Hash = @{
    Key1 = ''
    Key2 = ''
    HashKey1 = @{
        Key3 = ''
        Key4 = ''
    }
}

I'd like to validate that the hash that is provided matches this structure. Of course I can manually check with something like:

Compare-Object $Hash.Keys $InputHash.Keys
Compare-Object $Hash.HashKey1.Keys $InputHash.HashKey1.Keys

But this would only work when I manually enter the HashKey1 key name, since it's a nested hash--including any additional nested hashes.

Is there a way to dynamically compare the input hash with my own hash and verify the entire structure, including all nested hashes, matches?

Edit: For example, if I want my function to use the values provided in input hash, I want to first check and make sure the user even provided the correct hash.

function MyFunction ($InputHash){
    if ($InputHash.Fruits.Fruit1 -eq 'apples'){
        return 'apples'
    } else {
        return 'No apples'
    }
}
$Hash = @{
    Fruit1 = 'apples'
}
MyFunction -InputHash $Hash

In this example, my function is looking in the $InputHash hashtable for the 'Fruits' key, which itself is a hashtable containing the 'Fruit1' key. The user created a hash that only contains the 'Fruit1' key at the root, instead of inside the 'Fruits' hashtable key.

I need a way to provide my own hash with the key structure I want, and verify that what the user is providing matches that structure. The values don't need to match, just the key structure.

Edit: u/logicalmike figured out a simple way to do it that works.

https://www.reddit.com/r/PowerShell/comments/w5l84f/how_to_validate_multiple_levels_of_hashtable_keys/ih9172j/

$NoValuesA = (($HashA | ConvertTo-Xml -As stream) -split "\n" | where {$_ -notmatch 'Name="Value"'} ) -join ""
$NoValuesB = (($HashB | ConvertTo-Xml -As stream) -split "\n" | where {$_ -notmatch 'Name="Value"'} ) -join ""

$NoValuesA -eq $NoValuesB