Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4606142
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:31:31+00:00 2026-05-22T00:31:31+00:00

Can someone please explain Why my first examples don’t work, and why adding in

  • 0

Can someone please explain Why my first examples don’t work, and why adding in a ForEach-Object solves the problem? Thanks in advance!


I parsed the return from a command into a hashtable (sample at end of post) and want to log the information to a file as part of my processing. I know that $ht.GetEnumerator() | Sort-Object Name will return the full hash to screen, sorted. However, once I try sending things to file, it breaks.

$ht | Add-Content log.txt

only logs a single row of System.Collections.Hashtable. So, I’ve also tried

$ht.GetEnumerator() | Sort-Object Name | Add-Content log.txt 

and end up with rows of

System.Collections.DictionaryEntry
System.Collections.DictionaryEntry
System.Collections.DictionaryEntry

So then I tried to loop through and handle each individually with

foreach ($key in $ht.keys) {
Add-Content log.txt "$key : $ht.$key" }

and end up with

Server address : System.Collections.Hashtable.Server address
Client address : System.Collections.Hashtable.Client address
User name : System.Collections.Hashtable.User name

Solved with:

$ht.GetEnumerator() | Sort-Object Name |
ForEach-Object {"{0} : {1}" -f $_.Name,$_.Value} |
Add-Content log.txt 

For reference, the hashtable sample:

$ht = @{
    "Server address" = "server.net";
    "Client address" = "10.20.121.153";
    "User name" = "myuser"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:31:32+00:00Added an answer on May 22, 2026 at 12:31 am

    Answering the why part, you obviously have a solution 🙂

    In your first example

    $ht | Add-Content log.txt
    

    PowerShell takes $ht and tries to somehow convert it to a string so that it can be stored via Add-Content. Because there is no conversion defined for the hashtable, only the type name is returned from the conversion. Same as for example new-Object Random|Add-Content d:\log.txt. Again, only type name is written.

    Next

    $ht.GetEnumerator() | Sort-Object Name | Add-Content log.txt 
    

    is similar. GetEnumerator returns object that is used for iteration; objects of type System.Collections.DictionaryEntry are returned. Again, there is no conversion to string, so type names are returned.

    Personally, I think PowerShell should be smart enough and help here. The question is “how?”. Designers probably didn’t want to hardcode the output. It might be "{key}: {value}" or "{key} = {value}", or "{key}/{value}", or … The format is not clear, so they left it for us to decide and format as you did it with the foreach statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain the following How was the first JDK release unit Tested?
Can someone please explain the below to me. First is how I call the
Can someone please explain the difference between these two examples in the context of
Can someone please explain output 0 in case of first constructor without this ?
Can someone please explain why ?___SID=U is appearing in some Magento URLs on my
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Can someone please explain to me how this responsive approach works? This was done
Can someone please explain to me why the hell? When I try to set
Can someone please explain this phenomenon? I am using Mojarra 2.1.6 - Glassfish 3.1.2.
Can someone please explain to me difference between extends and super in java generics

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.