PowerShell Snippet #12 - Creating Arrays of HashTables in PowerShell

Hello!

As part of SQLDWSchemaMigrate there is a function called Compare-Rows, which finds if there is a difference between the number of columns for a given table that exists on both source and target tables. And I need to return a hash table of the schema/table where there is a mismatch wrt column count.

What makes this a challenge is that hash tables cannot store duplicate keys. I was setting the key to the schema name, so theoretically I could just set the value to the schema and the key to the table. But this would still cause issues when there are tables with the same names in different schemas, which is a thing.

So I needed a different solution, which is to store an array of hash tables and loop through the array to get the details. This way I can store duplicate values.

https://github.com/sabinio/SQLDWSchemaMigrate/blob/master/SQLDWSchemaMigrate/Functions/CompareFunctions.ps1