3

I have code to output a sorted hashtable to the screen:

$h.getenumerator() | sort value -descending

It looks like this:

Name                           Value
----                           -----
10.10.10.10 69566308           151
10.10.10.11 69566308           143
10.10.10.12 69566308           112
10.10.10.13 69566308            99
10.10.10.14 69566308            71
10.10.10.15 69566308            70

But I would like to output this to a file instead.

When I try to output to a file with

$h.getenumerator() | sort value -descending | out-string | Add-Content D:\Script\iis_stats.log

or

$h.getenumerator() | sort value -descending | Add-Content D:\Script\iis_stats.log

All I get is "System.Collections.DictionaryEntry" in D:\Script\iis_stats.log.

How do I fix it?

3
  • How do you want to format the output? Commented Jan 9, 2015 at 20:05
  • Please take a step back and describe the actual problem you're trying to solve instead of what you perceive as the solution. What do the contents of your hashtable look like? Where does the data come from? What should the output look like? Commented Jan 9, 2015 at 20:06
  • @AnsgarWiechers Ok I edited Commented Jan 9, 2015 at 20:13

2 Answers 2

2

If you want it in the same format it displays on the screen (folded at the pipes for readability):

$h.getenumerator() | 
  Sort-Object value -descending | 
  Format-Table | 
  Out-String | 
  Add-Content D:\Script\iis_stats.log
Sign up to request clarification or add additional context in comments.

Comments

1

You would get similar output on your screen if you did something like this as well:

$h.GetEnumerator() | sort value -Descending | ForEach-Object{
    $_.GetType().FullName
}

For every entry you would see System.Collections.DictionaryEntry since it does not have a proper string equivalent.

Then you should just do what mjolinor suggests. PowerShell automatically appends the | Out-Default command to the console so that it displays in a way that is visually pleasing. When you send the output to Add-Content that prettification cannot occur.

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.