Formatting output from an array.

This topic contains 9 replies, has 4 voices, and was last updated by  Ben Collins 5 years, 2 months ago.

  • Author
  • #4722

    Ben Collins

    I am creating an object, adding some properties, then displaying the results

    ForEach ($item in $data){

    $objInfo = New-Object System.Object
    $objInfo | Add-Member -MemberType NoteProperty -Name 'Label1' -Value $item.data1
    $objInfo | Add-Member -MemberType NoteProperty -Name 'Label2' -Value $item.data2
    $objInfo | Add-Member -MemberType NoteProperty -Name 'Label3' -Value $item.data3
    $objInfo | Add-Member -MemberType NoteProperty -Name 'Label4' -Value $item.data4

    $Info += $objInfo


    The last property,  data4 potentially has multiple properties.   When displaying the results, that property displays as an array.

    {Property1, Property2, Property3}

    What I can't seem to work out is how to only display the elements,  without the {}.   It's a purely cosmetic thing,  as functionally it works fine.  Any suggestions?


  • #4733

    Art Beane

     $item.data4 is an array and you'll need to convert it to a string in order to display it the way you want. Try this:

    ForEach ($item in $data) {
        $objInfo = New-Object PSObject -Property @{
            'Label1' = $item.data1
            'Label2' = $item.data2
            ‘Label3' = $item.data3
            'Label4' = $item.data4 -Join ", "
        $Info += $objInfo
  • #4734

    Rob Campbell

    If it's a string array, you can cast it as [string], and it will create a single string from the array.   If you want some sort of delimiter or separator character between them, you can either set the $OFS automatic variable to whatever you want the delimiter to be before you do that cast, or you can run the array through -join and specify the separator there.

  • #4735

    Jeffrey Smith

    If you just want a cosmetic way to display the data, you can do something like:


    $item.data4 -join ', '

    This will display all items in the array, separated by a comma.

  • #4760

    Ben Collins

    I appreciate the responses.   however I am not sure how exactly to implement this.

    Once the object is created,  I simply have a line that that displays the contents of the object:


    How exactly would I cast this as a string?   I want to keep existing output,  which displays as a table.


  • #4766

    Art Beane

    $Info is a "proper" PowerShell object that you can process however you want. For example, to output it to the console as a table, use $Info | Format-Table -AutoSize -Wrap.

    To output it to the screen as a table, use $Info | Out-GridView

    To output it to Excel, use $Info | Export-Csv -NoTypeInformation

  • #4767

    Rob Campbell

    How exactly would I cast this as a string? I want to keep existing output, which displays as a table.

    What you are seeing is Powershell's default display format for properties that are arrays. If you want to keep that property an array, then you'll need to do your own formatting when you output it, or create your own object type. Then you can create your own custom default format for that object type in the .ps1xml file.

  • #4768

    Ben Collins

    I might not be explaining this well.   There is a bit more to the script,   it basically imports an XML configuration file,  where data4 can have multiple values.    The default format is fine,    it does format as a table already,  which is exactly what I wanted to begin with.   If I simply display the contents of  my object which is $Info,  I get this:


    Label1      Label2     Label3     Label4
    ——–      ———     ——–     ———

    data1        data2        data3       {data4, data4, data4}

    This is exactly what I want,   with the exception of the properties of data4 being displayed as an array.   If I need to change it to a string,  that's perfect,  but how do I go about casting just that portion of my object as a string when creating the output?

  • #4771

    Rob Campbell

    $info |
    select Label1,Label2,Label3,@{Name='Label4';Expression={[string]$_.Label4}} |
    Format-Table -AutoSize

  • #4773

    Ben Collins

    [quote=4771]$info |
    select Label1,Label2,Label3,@{Name=’Label4′;Expression={[string]$_.Label4}} |
    Format-Table -AutoSize[/quote]

    Excellent!  Thank you Rob,  exactly what I was looking for!

You must be logged in to reply to this topic.