Note: I'm assuming that you're looking for a friendlier display representation of your data. For programmatic processing, Format-*
cmdlets should be avoided, for the reasons explained in this answer.
What you're looking for is for Format-List
to work recursively, i.e. to not only list the individual properties and their values for each input object itself, but also for nested objects contained in property values.
Format-List
does not support this:
- Nested objects are represented by their single-line
.ToString()
representations.
- If they're part of a collection (enumerable), the individual elements' representations are joined with
,
on a single line, and are enclosed in {...}
(!) as a whole. How many elements are shown at most is controlled by the $FormatEnumerationLimit
preference variable, which defaults to 4
.
However, you can approximate recursive listing behavior with Format-Custom
; using a simplified example:
# Nested sample object to format.
[pscustomobject]@{
userDetails = [pscustomobject] @{
id = 'AA:BB:CC:DD:11:22'
connectionStatus= 'CONNECTED'
hostType = 'WIRELESS'
authType = 'WPA2/WPA3+802.1x/FT-802.1x'
}
connectedDevice = '...'
} |
Format-Custom -Depth 1 # use higher -Depth levels for multi-level expansion
Output:
class PSCustomObject
{
userDetails =
[
class PSCustomObject
{
id = AA:BB:CC:DD:11:22
connectionStatus = CONNECTED
hostType = WIRELESS
authType = WPA2/WPA3+802.1x/FT-802.1x
}
]
connectedDevice = ...
}
Note:
Caveat: If a custom view happens to be defined for a given input object's type via associated formatting data, it is that custom view that Format-Custom
will invoke, not the structural representation shown above; however, this is rare ([datetime]
is a rare example).
Apart from the output showing the structure recursively, the format differs from that of Format-List
as follows:
- Complex objects are enclosed in
class <typeName> { ... }
- Elements of collections (enumerables) each render on their own (group of) line(s), enclosed in
[ ... ]
overall. However, as with Format-List
, the number of elements that are shown at most is limited by $FormatEnumerationLimit
.
To prevent excessively nested output, Format-Custom
stops recursing at a depth of 5
by default; you can control the recursion depth via the -Depth
parameter, 1
meaning that only objects in immediate child properties are expanded.
When the recursion depth limit is reached, non-collection objects are represented by their .ToString()
representations, as with Format-List
.