Formatting output from an array.

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?

 

 $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
}

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.

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.

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:

$Info

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

 

$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 <full-path-to-info.csv> -NoTypeInformation

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.

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?

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

[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!