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
  • 6,163
  • 6
  • 56
  • 83

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
  • 70,406
  • 17
  • 130
  • 175
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
  • 290,304
  • 63
  • 469
  • 417