Format nested hash table

For a hash table containing heterogeneous data, such as:

$items = @{ a = @{a1 = "A1"; a2 = "A2"; a3 = "A3" } b = 1234 c = @{c1 = "C1"; c2 = "C2"; c3 = "C3" } d = [DateTime]::Now } 

When I try to display the content using the following:

 $items | Format-Table -AutoSize 

Conclusion:

 Name Value ---- ----- c {c3, c1, c2} d 05/23/15 11:37:56 b 1234 a {a2, a3, a1} 

But how can I expand the contents of nested hash tables so that I can see key-value pairs, such as:

 Name Value ---- ----- c {c3=C3, c1=C1, c2=C2} d 05/23/15 11:37:56 b 1234 a {a2=A2, a3=A3, a1=A1} 

The exact format for displaying nested key-value pairs is not very critical, I just want to see them.

+5
source share
2 answers

You need to expand the nested hash tables:

 $items | Format-Table Name, @{n='Value';e={ if ($_.Value -is [Hashtable]) { $ht = $_.Value $a = $ht.keys | sort | % { '{0}={1}' -f $_, $ht[$_] } '{{{0}}}' -f ($a -join ', ') } else { $_.Value } }} 
+4
source

This is not very good, but ConvertTo-Xml -As String can display nested data structures to arbitrary depth:

 $items | ConvertTo-Xml -As String 
0
source

All Articles