Is it possible to format the output of a hashtable in Powershell to output all the values onto one line?
e.g.
I have the hash table $hashErr with the below values:
$hashErr = @{"server1" = "192.168.17.21";
"server2" = "192.168.17.22";
"server3" = "192.168.17.23"}
Which are written to a log with the below:
$hashErr.GetEnumerator() | Sort-Object Name | ForEach-Object {ForEach-Object {"{0}`t{1}" -f $_.Name,($_.Value -join ", ")} | Add-Content $log
The will cause the below to be written to the log:
Name Value
---- -----
server2 192.168.17.22
server1 192.168.17.21
server3 192.168.17.23
My question is, how can I format this hash table so the output is written all to one line, like the below?
server2 192.168.17.22 | server1 192.168.17.21 | server3 192.168.17.23
This could be done by looping through all the values in the hash table and putting them into an array but surely there is a more direct way?
ForEach-Object {
or a missing}
(with either change the code works way but the outer ForEach-Object isn't needed). – Pitchblack$hashErr.GetEnumerator() | Sort Name
by itself produces the output in his listing. In fact, doing it with the Foreach-Object loop/formatted strings wouldn't even write the column headings. user_invalid, where did those headings come from? Did you add them to the log manually beforehand? – ResidentPS M:\> $hashErr
– Lefler