The following code is display the data I want, but it's not aligned nicely. How can I use a data-grid or format-table to align the data?
$response = Invoke-RestMethod @params
foreach ( $row in $response )
{
Write-host "id=$($row.uuid) uuid=$($row.uuid) Subject=$($row.subject) "
}
Example output now:
id=new-template_1 uuid=new-template_1 Subject=Welcome!
id=new-template_3 uuid=new-template_3 Subject=Welcome!
Desired:
id uuid subject
new_template_1 new_template_1 Welcome!
new_template_3 new_template_3 Welcome!
CodePudding user response:
If you just want a "trimmed" set of properties from the objects output by Invoke-RestMethod
, use Select-Object
:
$response = Invoke-RestMethod @params
$trimmedResponse = $response |Select-Object @{Name='id';Expression='uuid'},uuid,subject
# This will now default to table view formatting
$trimmedResponse
# But you can also explicitly pipe to `Format-Table`
$trimmedResponse |Format-Table
# ... allowing you greater control over the formatting behavior
$trimmedResponse |Sort-Object Subject |Format-Table -GroupBy Subject
The first argument passed to Select-Object
(@{Name='id';Expression='uuid'}
) is known as a calculated property - in this case it defines a new property named id
, with the value of the uuid
property of the input object.