# Adding rows to hash table?

**URL:** <https://forums.powershell.org/t/adding-rows-to-hash-table/16738>\
**Category:** PowerShell Help\
**Created:** [June 3, 2021, 9:51pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738 "2021-06-03T21:51:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 3, 2021, 9:51pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/1 "2021-06-03T21:51:30Z")

</div>

i have a list of FQDNs, i want to create a hash table. in each row the name column is “dns” and the value column is one of the fqdns.

i was previously removing the domain from the fqdn and including both the short name and fqdn in the output. so this works:

```auto
foreach($entry in $list)
    {
    $thiscerthost = $entry.fqdn.replace('.domain.com','')
    $thiscertSANs += @{'dns' = $entry.fqdn},@{'dns'= ($thiscerthost)} # include short name
    }

```

but now i don’t want the short name in it. so i took off the second part of the second line. but now i get errors on every row in the source about the dns key already existing.

```auto
foreach($entry in $list)
    {
    $thiscertSANs += @{'dns' = $entry.fqdn} # exclude short name
    }

```

Item has already been added. Key in dictionary: ‘dns’ Key being added: ‘dns’

i’m sure there’s a better way to do this, but i’m confused why the way i’m doing it worked when i was adding two

@{something},@{somethingelse}

but doesn’t work with only

@{something}.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 3, 2021, 10:06pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/2 "2021-06-03T22:06:59Z")

</div>

I’m not sure if I really got what you want to do but I guess using a `PSCustomObject` might help in this case:

```auto
$thiscertSANs =
foreach ($entry in $list) {
    [PSCustomObject]@{
        dns = $entry.fqdn
    }
}
$thiscertSANs

```

---

<div class="post-metadata">

**Author:** ![dicey](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dicey](https://forums.powershell.org/u/dicey)\
**Post date:** [June 4, 2021, 5:33am UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/3 "2021-06-04T05:33:30Z")

</div>

@Olaf’s answer provides a solution as it creates a hash table. But I enjoy these sorts of puzzles as they can provide experience.

@john-curtiss - I believe the error you received is due to hash tables using name/value pairs as the index, unlike arrays which use sequential numbers. You might have duplicates in either your source or transformed data.

My curiosity was also piqued by `$entry.fqdn`. This indicates that `$list` is already in some form of collection like `[DataTable]`. And so, thinking outside the box, I created a 6x2 box for the source data. Then I put a subset of those data into a dictionary object where the **key** is the fqdn URI. The **value** in each row is _dns_ which can be described as “type” or some such metadata later.

Subtext: use fqdn as the unique key in a collection. Extract the keys based on the generalized value.

Dictionary versus hash table discussions litter the internet. I like using dictionaries as they are well structured and should beat a hash table in data retrieval when sizes grow large

As for the multidimensional array _`list`_, I like MDAs for well-structured data because of the way the data are stored. And, since the indexes are integers, you can get funky with data storage using loops. --When I squint (really squint) at MDAs, I see T-SQL’s `hierarchyid` and `FOR XML EXPLICIT`.

```powershell
<#
Key is unique; use FQDN as URI.

Shape:

key value
------------
<fqdn> 'dns'
<fqdn> 'dns'
<fqdn> 'dns'
<fqdn> 'dns'
<fqdn> 'dns'

#>
using namespace System.Collections.Generic;
using namespace System.Data;

$list = [string[,]]::new(6,2); # 6 rows, 2 columns;
    $list[0,0] = 'AutoDiscover';
    $list[0,1] = 'autodiscover.domain.com';
    $list[1,0] = 'ActiveSync';
    $list[1,1] = 'activesync.domain.com';
    $list[2,0] = 'Mail Services';
    $list[2,1] = 'mailservices.domain.com';
    $list[3,0] = 'om-xchangca';
    $list[3,1] = 'om-xchangca.domain.com';
    $list[4,0] = 'owa';
    $list[4,1] = 'owa.domain.com'
    $list[5,0] = '<none>';
    $list[5,1] = 'domain.com';

$thiscertSANs = [Dictionary[string,string]]::new();

for($i = 0;$i -lt $list.GetUpperBound(0); $i++)
{
  $thiscertSANs.Add("$($list[$i,1])",'dns');
}

## Results
cls;
$thiscertSANs; # or $thiscertSANs.GetEnumerator();
echo "`n`n";
($thiscertSANs.Keys).Where({$thiscertSANs.Values -eq 'dns'});

```

## Results

 ![Results](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/e/e743875b4dda71187e0cafb97f97158eeaf4372a.jpeg)

---

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 4, 2021, 12:44pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/4 "2021-06-04T12:44:51Z")

</div>

$list is imported from a csv that has many more columns than just FQDN, and other parts of my script are doing other things to other parts of the csv.  
but, for testing, given a csv with two columns called “column” and “FQDN”, where column has anything and FQDN has [this.domain.com](http://this.domain.com) and [that.domain.com](http://that.domain.com).

```auto
PS C:\Users\john.curtiss [06/04/2021 07:35:10]> $list

column fqdn           
------ ----           
anything this.domain.com
anythingelse that.domain.com

```

the following code produces a hash table

```auto
foreach($entry in $list)
    {
   $thiscerthost = $entry.fqdn.replace('.domain.com','')
   #$thiscertSANs += @{'dns' = $entry.fqdn} #exclude short name
   $thiscertSANs += @{'dns' = $entry.fqdn},@{'dns'= ($thiscerthost)} # include short name
    }

```

which looks like this:

```auto
PS C:\Users\john.curtiss [06/04/2021 07:39:27]> $thiscertSANs

Name Value                                                                                                                                                                                                          
---- -----                                                                                                                                                                                                          
dns this.domain.com                                                                                                                                                                                                
dns this                                                                                                                                                                                                           
dns that.domain.com                                                                                                                                                                                                
dns that                        

```

i need the same hash table with only the FQDNs. i have tried the following code

```auto
foreach($entry in $list)
    {
   #$thiscerthost = $entry.fqdn.replace('.domain.com','')
   $thiscertSANs += @{'dns' = $entry.fqdn} #exclude short name
   #$thiscertSANs += @{'dns' = $entry.fqdn},@{'dns'= ($thiscerthost)} # include short name
    }

```

but it gives me “Item has already been added. Key in dictionary: ‘dns’ Key being added: ‘dns’” and produces a hash table that does not contain both FQDNs:

```auto
PS C:\Users\john.curtiss [06/04/2021 07:42:29]> $thiscertSANs

Name Value                                                                                                                                                                                                          
---- -----                                                                                                                                                                                                          
dns this.domain.com       

PS C:\Users\john.curtiss [06/04/2021 07:43:51]> $thiscertSANs | Get-Member

   TypeName: System.Collections.Hashtable

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 4, 2021, 1:17pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/5 "2021-06-04T13:17:37Z")

</div>

Have you tried my code suggestion?

---

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 4, 2021, 1:19pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/6 "2021-06-04T13:19:52Z")

</div>

@olaf, yes, it produces an object that doesn’t look like my hash table; and get-member says it is something other than a hash table.

```auto
$thiscertSANs

dns            
---            
this.domain.com
that.domain.com

PS C:\Users\john.curtiss [06/04/2021 08:18:42]> $thiscertSANs | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 4, 2021, 1:34pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/7 "2021-06-04T13:34:16Z")

</div>

OK … but yours is actually an array of single value hashtables and not a “proper” hashtable. What is it actually what you’re trying to do? Whatfor do you need such a weird hashtable collection?

Just to show what I mean.  
Your type of “hashtable”:

```auto
$Hashtable = @(
@{dns = 'this.domain'},
@{dns = 'that.domain'}
)
$Hashtable | Get-Member

```

A proper hashtable:

```auto
$ProperHashtable = @{
    dns1 = 'this.domain'
    dns2 = 'that.domain'
}
$ProperHashtable | Get-Member

```

With the proper hashtable you can access the values by specifying the according key:

```auto
$ProperHashtable.dns2

```

With your way you wouldn’t be able to pick one single element:

```auto
$Hashtable.dns

```

You always get all of them.

---

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 4, 2021, 1:43pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/8 "2021-06-04T13:43:12Z")

</div>

i am passing $thiscertsans as a parameter to another command that requires it to be a hashtable. it’s adding the FQDNs as subject alternative names to an SSL certificate it’s creating via an API.

so if i establish $thiscertsans as an array first, this seems to work. i guess i didn’t have to do that before because i was adding two items at a time and it automatgically assumed it was an array. :

```auto
$thiscertSANs = @()
foreach($entry in $list)
    {
    $thiscertSANs += @{'dns' = $entry.fqdn} #exclude short name
     }

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 4, 2021, 1:55pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/9 "2021-06-04T13:55:52Z")

</div>

> [@john-curtiss](#):
>
> another command that requires it to be a hashtable

Is that something you wrote yourself? Can you show it? Maybe you make it harder that needed.

---

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 4, 2021, 3:29pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/10 "2021-06-04T15:29:39Z")

</div>

no, it is not.  
[https://venafitppps.readthedocs.io/en/latest/functions/New-TppCertificate/#-subjectaltname](https://venafitppps.readthedocs.io/en/latest/functions/New-TppCertificate/#-subjectaltname)

reading that, it does say i can use “multiple hashtables,” which i assume is what an array of hashtables is, which is what i assume was happening automagically before when i was adding two “rows” at a time. i was confused because i was getting “TypeName: System.Collections.Hashtable” from get-member.

if i pass your version of $thiscertsans to that venafi command, it says

“New-TppCertificate : Cannot process argument transformation on parameter ‘SubjectAltName’. Cannot convert value “@{[dns=this.domain.com](http://dns=this.domain.com)}” to type “System.Collections.Hashtable”. Error: “Cannot convert the “@{[dns=this.domain.com](http://dns=this.domain.com)}” value of  
type “System.Management.Automation.PSCustomObject” to type “System.Collections.Hashtable”.””

---

<div class="post-metadata">

**Author:** ![dicey](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dicey](https://forums.powershell.org/u/dicey)\
**Post date:** [June 4, 2021, 6:38pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/11 "2021-06-04T18:38:55Z")

</div>

@john-curtiss - Good news! I replicated your error before coming up with this approach!

The _SubjectAltName_ parameter might introduce a few more programmatic issues because of its attributes, mainly _Accept pipeline input_. Therefore, you could attack this by generating a string that you’d paste into the cmdlet.

```powershell
<#
Desired
-SubjectAltName @{'Email'='me@x.com'},@{'IPAddress'='1.2.3.4'}
#>
using namespace System.Collections;

## Create csv source data
$list = Import-Csv A:\sourceData.csv -Header "column","fqdn";

## Create and populate a collection object to store dynamic strings
$thiscertSANs = [ArrayList]::new();
foreach($entry in $list)
{
    [void]$thiscertSANs.Add( $("'{0}'='{1}'" -f $entry.column,$entry.fqdn));
}

## Results
($thiscertSANs|%{'@{'+$_+'}'}) -join","

```

A string of hash tables is constructed to be pasted into the _SubjectAltName_ parameter.

![Results](https://us1.discourse-cdn.com/flex019/uploads/powershell/optimized/2X/5/5f5865127cb7059e0b4775cea498ed8ce91c649b_2_517x30.jpeg)

Because this is an ArrayList, you can call an instance with a simple index value (this functionality can be magicized). For example, `'@{'+$thiscertSANs[-1]+'}'` will generate a hashy-string for the last instance.

![Results03](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/3/3ae8a1fb25a10228847be3c3c1e10451a2f1d0fe.jpeg)

## Notes

- The `Add()` method for the ArrayList does not allow for the hash table declaration in the sting, and must be added later.
- Using `[Dictionary[int,hashtable]]` raises the error (or similiar) in the original post. _–which is odd. Maybe I fat-fingered somewhere._

### Source Data

 ![SourceData](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/4/40f0ac9a98daad7b766174aa8b1c40d4069447d6.jpeg)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 4, 2021, 9:02pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/12 "2021-06-04T21:02:50Z")

</div>

Hmmm … ok at least you could make your way a little easier to read I think:

```auto
$List = @'
"fqdn","bla"
"this.domain.com","blu"
"that.domain.com","ble"
"some.domain.com","blo"
'@ | 
ConvertFrom-Csv -Delimiter ','

$thiscertSANs =
foreach ($entry in $List) {
    @{dns = $entry.fqdn}
}

```

---

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 8, 2021, 3:43pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/13 "2021-06-08T15:43:16Z")

</div>

now. how do i remove duplicate rows from $thiscertSANs? get-unique and sort -unique are not doing it for me. they are only returning a single result.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 8, 2021, 3:52pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/14 "2021-06-08T15:52:08Z")

</div>

> [@john-curtiss](#):
>
> how do i remove duplicate rows from $thiscertSANs?

Easy. You do it before you turn them into an array of hashtables … 😉

```auto
$List = @'
"fqdn","bla"
"this.domain.com","blu"
"that.domain.com","ble"
"some.domain.com","blo"
"that.domain.com","bli"
"this.domain.com","blö"
"some.domain.com","blü"
'@ | 
ConvertFrom-Csv -Delimiter ',' |
Sort-Object -Property 'fqdn' -Unique

$thiscertSANs =
foreach ($entry in $List) {
    @{dns = $entry.fqdn }
}

```

😉

---

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 8, 2021, 4:07pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/15 "2021-06-08T16:07:16Z")

</div>

```auto
$thiscertSANs | sort {$_.values} -Unique
$thiscertSANs | select {$_.values} -unique

```

these seem to work.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 8, 2021, 4:19pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/16 "2021-06-08T16:19:28Z")

</div>

> [@john-curtiss](#):
>
> these seem to work.

Cool. Now you have 3 options to choose from. 👍🏽 😉

---

<div class="post-metadata">

**Author:** ![dicey](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dicey](https://forums.powershell.org/u/dicey)\
**Post date:** [June 8, 2021, 6:53pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/17 "2021-06-08T18:53:28Z")

</div>

[This script](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/14) might add to the work @john-curtiss will perform in providing the automation which includes the New-TppCertificate cmdlet.

@john-curtiss, amend my second post as such:

```powershell
using namespace System.Collections;
$list = Import-Csv A:\sourceData.csv -Header "column","fqdn"|Select-Object -Unique column,fqdn;

$thiscertSANs = [ArrayList]::new();
foreach($entry in $list)
{
    [void]$thiscertSANs.Add( $("'{0}'='{1}'" -f $entry.column,$entry.fqdn));
}

$output = ($thiscertSANs|%{'@{'+$_+'}'}) -join",";

```

This provides you with a string variable, `output`, of hashed data (includes the required _dns_ label for the cmdlet) that you can attempt to pass as the _SubjectAltName_ parameter value. Piping the variable is not likely to work because of the param attributes, but try scripting `$output` as the parameter value directly in the cmdlet. I do not know if it will be interpreted as a series of hash tables, but I am banking on the format in the cmdlet’s Example 3 and

> You can provide more than 1 of the same SAN type with multiple hashtables.

I added duplicate entries into the source data and included a Select…Unique command. Notice that there is not a header row in the content–I tend to supply my own headers for the data during `Import-Csv`. The source data file now contains:  
 ![sourceData](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/3/3262139a0a1cfd1c6d4b4dee7eb7e5f37d7852cb.jpeg)

---

<div class="post-metadata">

**Author:** ![john-curtiss](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@john-curtiss](https://forums.powershell.org/u/john-curtiss)\
**Post date:** [June 8, 2021, 9:04pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/18 "2021-06-08T21:04:00Z")

</div>

this one is doing what i need:

```auto
$thiscertSANs = @()
foreach($entry in $list)
    {
    $thiscertSANs += @{'dns' = $entry.fqdn} #exclude short name
     }

```

i’m able to include $thiscertsans in new-tppcertificate this way. thanks very much, guys.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 8, 2021, 9:12pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/19 "2021-06-08T21:12:34Z")

</div>

> [@john-curtiss](#):
>
> this one is doing what i need:

Again … .the slightly better way of collecting elements in an array would be this:

```auto
$thiscertSANs = 
foreach ($entry in $list) {
    @{'dns' = $entry.fqdn } 
}

```

Because PowerShell actually does not have a method for adding elements to an array it’s destroying the original array and creates a new one with the same name and with the elements of the original one plus the one you asked it to add with “ **+=** ”. That takes more memory and and more time than simply assigning the output of elements to a variable. 😉 … and - at leat for me - I think it’s a little easier to read.

---

<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:24pm UTC](https://forums.powershell.org/t/adding-rows-to-hash-table/16738/20 "2024-05-16T20:24:14Z")

</div>

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


