0

I'm having a problem, it looks simple but can't find a solution; I am iterating a hash table in PowerShell it looks like this:

foreach($tool in $otherTools.GetEnumerator() | Sort Name)
{
   #tried echo $tool.Name
   #tried echo $tool.Value
   #tried echo $tool.Key
}

but nothing above works. The reason is I want to use the switch statement inside by key.

HashTable looks like:

Name                           Value
----                           -----
One                           testone
Two                           TestTwo
1
  • 1
    Unclear question. What's not working? What output are you getting? Errors? Commented May 29, 2016 at 11:41

1 Answer 1

4

You are doing it right. Maybe you used something like echo "$tool.Key" which will output System.Collections.DictionaryEntry.Key. To fix that, you could either use echo "$($tool.Key)" or a format string:

$hashTable = @{
    One = 'testone'
    Two = 'TestTwo'
}

foreach ($tool in $hashTable.GetEnumerator() | sort Name)
{
    Write-Host ("Name: {0} Value: {1}" -f $tool.Key, $tool.Value)
}

Output:

Name: One Value: testone
Name: Two Value: TestTwo
Sign up to request clarification or add additional context in comments.

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.