expirat001
expirat001

Reputation: 2195

Using Format-Table with pscustomobject

I want to use Format-Table -Autosize with pscustomobject.

I want the equivalent of :

Get-Process | ft Id,ProcessName -AutoSize

I tried (although the output is located in the center)

Get-Process | %{
            [pscustomobject]@{
                ID   = $_.Id
                ProcessName  = $_.ProcessName
            }
}

It works but when I use Format-Table -Autosize it is not working, it add new titles with new lines.

Get-Process | %{
            [pscustomobject]@{
                ID   = $_.Id
                ProcessName  = $_.ProcessName
            } | Format-Table -AutoSize
}

How to resolve this problem?

Upvotes: 1

Views: 5870

Answers (2)

Matt
Matt

Reputation: 46710

You're pipe is in the wrong location.

Get-Process | %{
            [pscustomobject]@{
                ID   = $_.Id
                ProcessName  = $_.ProcessName
            } 
} | Format-Table -AutoSize

You were telling it to output a single table for every element instead of using the pipeline as intended.

Upvotes: 2

Bacon Bits
Bacon Bits

Reputation: 32170

You're piping to Format-Table at the wrong point:

Get-Process | % {
    [pscustomobject]@{
        ID   = $_.Id
        ProcessName  = $_.ProcessName
    } 
} | Format-Table -AutoSize

Upvotes: 2

Related Questions