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 "|"
Not excat output as you want but also 1 line output.
$hashErr | ConvertTo-Json -Compress
outputs:
{"server2":"192.168.17.22","server3":"192.168.17.23","server1":"192.168.17.21"}
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