Table Formatting

Hi there,

I am trying to format my tables similarly to the screenshot below with the properties stacked vertically and the value to the right of it. I feel I am overthinking this and it should be a lot easier. Any help would be appreciated.

Here’s an example using a hashtable.

        $Props = @{
            "Property1" = 123
            "Property2" = 123
            "Property3" = 123
            "Property4" = 123
        }.GetEnumerator() | Select-Object @{l='key'; e={$_.Key} }, @{l='value'; e={$_.Value} }

        New-UDTable -Data $Props -Columns @(
            New-UDTableColumn -Property 'Key' -Title 'Key'
            New-UDTableColumn -Property 'Value' -Title 'Value'
        )

This is how you could do it with an object.


        $Props = ([PSCustomObject]@{
            "Property1" = 123
            "Property2" = 123
            "Property3" = 123
            "Property4" = 123
        }).PSObject.Properties | Select-Object @{l='key'; e={$_.Name} }, @{l='value'; e={$_.Value} }

        New-UDTable -Data $Props -Columns @(
            New-UDTableColumn -Property 'Key' -Title 'Key'
            New-UDTableColumn -Property 'Value' -Title 'Value'
        )

Wow, such a quick and amazing response. You are very much appreciated thank you Adam.

1 Like

Works like a charm with data returned by a cmdlet. not so much with data returned from rest

So far my best attempt is something like this:

$props = Invoke-RestMethod "https://somethinghere/$($Eventdata.UserPrincipalName)" -Method GET
                   
                    
New-UDTable -Title "All Room Details" -Columns @(
    New-UDTableColumn -Property 'Key' -Title 'Key'
    New-UDTableColumn -Property 'Value' -Title 'Value'
) -Data ($props | Get-Member -MemberType Property,noteproperty | Select-Object @{l='key'; e={$_.Name} }, @{l='value'; e={$props | Select-Object -ExpandProperty $_.name} })

works for most data, but i’m having a wee bit of trouble with properties that contain other objects

I guess that is to be expected, need to convert the data to a string for those particular properties

definetly not a mess :smiley: but it works

New-UDTable -Title "All Room Details" -Columns @(
    New-UDTableColumn -Property 'Key' -Title 'Key'
    New-UDTableColumn -Property 'Value' -Title 'Value'
  ) -Data ($props | Get-Member -MemberType Property,noteproperty | Select-Object @{l='key'; e={$_.Name} }, @{l='value'; e={
    if($_.name -eq "Emailaddresses"){
        ($props | Select-Object -ExpandProperty $_.name) | out-string
    } 
    else 
    {
        $props | Select-Object -ExpandProperty $_.name
    }
  } })