Home > Enterprise >  Convert Single Line to Multiple Lines
Convert Single Line to Multiple Lines

Time:07-24

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 .userDetails to select a child object (similar to Select-Object -ExpandProperty userDetails).
  • Using Format-List -Property to output a list of the given properties
  • Using Out-String to create a string from the formatting data that is produced by Format-List. This string looks exactly like the output you normally see on the console.
  • Use String method .Trim() to remove whitespace (in this case newlines) from the beginning and end.
  • Use the -replace operator 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 $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.

  • Related