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"

2 Answers 2

2

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

foreach ($key in $hh.keys)
{
  Write-Host $hh[$key]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I simplified it to $hh.keys | %{ $hh.$_ }
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.