Click here to Skip to main content
15,885,141 members
Articles / Desktop Programming / MFC
Technical Blog

Format Text Out as a Table (Format-Table) in PowerShell

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
20 Sep 2013CPOL 41.6K   1  
Format text out as a table (format-table) in PowerShell

Format-Table in a powerful command that you can use to format your output. Output can be a console or a file out. Format-Table works with object or an object collection.

For example, if you have an object collection from your code, you can output as a table.

C++
Get-SPSite  -Limit All | Format-Table –AutoSize
  • AutoSize will be used to format columns depending on screen size. But if you are working with long width tables, you better use Out-String -Width 4000 with output pipe. (4000 is a character count in horizontal. You can mention any suitable value.)

Example

C++
Get-SPSite  -Limit All| Format-Table –AutoSize | Out-String -Width 4000

image

You can use Format-Table with a single object as well.

Example: With all columns

C++
$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "saman"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 23423
$objAverage | Format-Table –AutoSize

image

Example: With mentioned columns

C++
$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "saman"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 23423
$objAverage | Add-Member -type NoteProperty -Name Col3 -Value 123

$objAverage | Format-Table Col1,Col2 –AutoSize

image

You can also create object collection, add objects and format it as a table.

C++
$table = @()
$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "aa"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 111
$objAverage | Add-Member -type NoteProperty -Name Col3 -Value 222
$table += $objAverage

$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "bb"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 333
$objAverage | Add-Member -type NoteProperty -Name Col3 -Value 444
$table += $objAverage
 
$table | Format-Table –AutoSize

image

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --