Format the output of a hash table in Powershell to output to one line Format the output of a hash table in Powershell to output to one line powershell powershell

Format the output of a hash table in Powershell to output to one line


You can iterate over the keys of a hash table, and then in the loop lookup the values. By using the pipeline you don't need an intermediate collection:

($hashErr.Keys | foreach { "$_ $($hashErr[$_])" }) -join "|"


The V4 version of Richard's solution:

$hashErr = @{"server1" = "192.168.17.21";              "server2" = "192.168.17.22";              "server3" = "192.168.17.23"}$hashErr.Keys.ForEach({"$_ $($hashErr.$_)"}) -join ' | 'server3 192.168.17.23 | server2 192.168.17.22 | server1 192.168.17.21


A bit late to the party, expanding on @mjolinor's solution, you can use the type accelerator [ordered]:

$hashErr = [ordered]@{"server1" = "192.168.17.21";          "server2" = "192.168.17.22";          "server3" = "192.168.17.23"}$hashErr.Keys.ForEach({"$_ $($hashErr.$_)"}) -join ' | 'server1 192.168.17.21 | server2 192.168.17.22 | server3 192.168.17.23

I think [ordered] was introduced in PS v. 3.0.