2

I have a hashtable of hashtables of key/value pairs (from a .ini file). It looks like this:

Name                           Value                                                                   
----                           -----                                                                   
global                         {}                                                                      
Variables                      {event_log, software_repo}                                               
Copy Files                     {files.mssql.source, files.utils.source, files.utils.destination, fil...

How can I output all of the key/value pairs in one hash table, instead of doing this?

$ini.global; $ini.variables; $ini."Copy Files"
Caleb Jares
  • 5,992
  • 6
  • 54
  • 82

2 Answers2

2

Given that $hh is you hashtable of hashtables you can use a loop :

foreach ($key in $hh.keys)
{
  Write-Host $hh[$key]
}
JPBlanc
  • 67,114
  • 13
  • 128
  • 165
2

You can easily write a function to recurse through an hash:

$hashOfHash = @{
    "outerhash" = @{
        "inner" = "value1"
        "innerHash" = @{
            "innermost" = "value2"
        }
    }
    "outer" = "value3"
}

function Recurse-Hash($hash){
    $hash.keys | %{
        if($hash[$_] -is [HashTable]){ Recurse-Hash $hash[$_]  }
        else{
            write-host "$_ : $($hash[$_])"
        }
    }
}

Recurse-Hash $hashOfHash

The above is just write-hosting the key values, but you get the idea.

manojlds
  • 275,671
  • 58
  • 453
  • 409