I am new to this Powershell.
I am trying to learn how to modified output.
When I run "Write-output $result | format-list" I have the following output
userDetails : @{id=AA:BB:CC:DD:11:22; connectionStatus=CONNECTED; hostType=WIRELESS;
authType=WPA2/WPA3 802.1x/FT-802.1x}
connectedDevice : {@{deviceDetails=}}
How do I rewrite this output to below using powershell 7.2 ? I would like to have
userDetails :
connectionStatus= CONNECTED
hostType = WIRELESS
authType = WPA2/WPA3 802.1x/FT-802.1x
connectedDevice :
Thank you for your help.
CodePudding user response:
Here is some code that produces output close to your desired output:
# Create sample data
$result = [pscustomobject] @{
userDetails = [pscustomobject]@{ id="AA:BB:CC:DD:11:22"; connectionStatus="CONNECTED"; hostType="WIRELESS"; authType="WPA2/WPA3 802.1x/FT-802.1x"}
connectedDevice = [pscustomobject]@{ deviceDetails=$null }
}
# Produce output
"userDetails :"
($result.userDetails |
Format-List -Property connectionStatus, hostType, authType |
Out-String).Trim() -replace ':', '='
"`nconnectedDevice :"
# TODO: add similar code as for .userDetails
Output:
userDetails :
connectionStatus = CONNECTED
hostType = WIRELESS
authType = WPA2/WPA3 802.1x/FT-802.1x
connectedDevice :
- Using member access
.userDetailsto select a child object (similar toSelect-Object -ExpandProperty userDetails). - Using
Format-List -Propertyto output a list of the given properties - Using
Out-Stringto create a string from the formatting data that is produced byFormat-List. This string looks exactly like the output you normally see on the console. - Use
Stringmethod.Trim()to remove whitespace (in this case newlines) from the beginning and end. - Use the
-replaceoperator to change the delimiter
CodePudding user response:
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$FormatEnumerationLimitpreference variable, which defaults to4.
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-Customwill 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-Listas 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 withFormat-List, the number of elements that are shown at most is limited by$FormatEnumerationLimit.
- Complex objects are enclosed in
To prevent excessively nested output,
Format-Customstops recursing at a depth of5by default; you can control the recursion depth via the-Depthparameter,1meaning 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 withFormat-List.
